-
Notifications
You must be signed in to change notification settings - Fork 0
/
pms.m
116 lines (102 loc) · 3.35 KB
/
pms.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* pms.m
*
* Created by Dave Carlton on 10/12/09.
* Copyright 2009 PolyMicro Systems. All rights reserved.
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include "pms.h"
int gDebugLevel = 1;
int gVerboseIndex = 0;
Boolean gVerboseStack[256];
UInt64 startTime = 0;
// PMLOGSetVerbose sets a new debug level
void PMLOGSetVerbose(Boolean value)
{ gDebugLevel = value; }
// PMLOGPushVerbose sets a new debug setting and saves the previous one
// Use this to reduce debug output inside loops etc.
void PMLOGPushVerbose(Boolean value) {
gVerboseStack[gVerboseIndex] = gDebugLevel;
gVerboseIndex++;
if (gVerboseIndex > 256)
gVerboseIndex = 256;
gDebugLevel = value;
}
// PMLOGPopVerbose restores the previous debug setting
void PMLOGPopVerbose(void) {
gVerboseIndex--;
if (gVerboseIndex < 0)
gVerboseIndex = 0;
gDebugLevel = gVerboseStack[gVerboseIndex];
}
static NSString*
_PMLOGFormat(id self, NSString * prefix, NSString * format) {
if(self != nil)
{
return [NSString stringWithFormat:@"%@ self(%@) %@", prefix, self, format];
}
else
{
return [NSString stringWithFormat:@"%@ %@", prefix, format];
}
}
void _PMLOGSELF(id self, NSString * format, ...)
{
NSString * finalFormat = _PMLOGFormat(self, @"PMLOG", format);
va_list ap;
va_start(ap, format);
NSLogv(finalFormat, ap);
va_end(ap);
}
void _PMMSG(const char *function, int inLevel, NSString * format, ...)
{
if( inLevel > gDebugLevel || inLevel == 0 )
{
// The level is not high enough to be displayed, we're skipping this item.
// inLevel = 0 = disabled PMLOG(0, @"log, but disabled");
// gDebugLevel = 1 = least verbose PMLOG(1, @"Routine log");
// gDebugLevel = 7 = most verbose PMLOG(5, @"Really detailed debug level");
return;
}
else
{
NSString * finalFormat = [NSString stringWithFormat:@"PMLOG: %s: %@", function, format];
va_list ap;
va_start(ap, format);
NSLogv(finalFormat, ap);
va_end(ap);
}
}
void _PMLOG(const char *file, const char *function, int inLevel, NSString * format, ...)
{
if( inLevel > gDebugLevel || inLevel == 0 )
{
// The level is not high enough to be displayed, we're skipping this item.
// inLevel = 0 = disabled PMLOG(0, @"log, but disabled");
// gDebugLevel = 1 = least verbose PMLOG(1, @"Routine log");
// gDebugLevel = 7 = most verbose PMLOG(7, @"Really detailed debug level");
return;
}
else
{
NSString * pathString = [NSString stringWithCString:file encoding:NSUTF8StringEncoding];
const char * fileString = [[pathString lastPathComponent] cStringUsingEncoding: NSUTF8StringEncoding];
NSString * finalFormat = [NSString stringWithFormat:@"PMLOG %s: %s: %@", fileString, function, format];
va_list ap;
va_start(ap, format);
NSLogv(finalFormat, ap);
va_end(ap);
}
}
void _PMERR(const char *file, const char *function, int err, NSString * format, ...) {
NSString * pathString = [NSString stringWithCString:file encoding:NSUTF8StringEncoding];
const char * fileString = [[pathString lastPathComponent] cStringUsingEncoding: NSUTF8StringEncoding];
NSString * finalFormat = [NSString stringWithFormat:@"PMLOG %s: %s: %@ - err: %x", fileString, function, format, err];
va_list ap;
va_start(ap, format);
NSLogv(finalFormat, ap);
va_end(ap);
}