settings subsystem now using lua
[chaz/yoink] / src / Moof / Log.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <cstdarg>
30 #include <cstdio> // snprintf
31 #include <cstring> // strcpy
32
33 #include "Log.hh"
34 #include "Script.hh"
35
36
37 namespace Mf {
38
39
40 static LogLevel logLevel_ = LOG_WARNING;
41
42 inline void printLog_(int logLevel, const char* fmt, va_list args)
43 {
44 if (logLevel_ < logLevel) return;
45
46 switch (logLevel)
47 {
48 case LOG_ERROR:
49 fprintf(stderr, " error: ");
50 break;
51 case LOG_WARNING:
52 fprintf(stderr, "warning: ");
53 break;
54 case LOG_SCRIPT:
55 fprintf(stderr, " script: ");
56 break;
57 case LOG_INFO:
58 fprintf(stderr, " info: ");
59 break;
60 case LOG_DEBUG:
61 fprintf(stderr, " debug: ");
62 break;
63 }
64
65 vfprintf(stderr, fmt, args);
66 fprintf(stderr, "\n");
67 }
68
69
70 LogLevel setLogLevel(LogLevel level)
71 {
72 if (level != 0)
73 logLevel_ = level;
74
75 return logLevel_;
76 }
77
78
79 void logError(const char* fmt, ...)
80 {
81 va_list args;
82 va_start(args, fmt);
83
84 printLog_(LOG_ERROR, fmt, args);
85
86 va_end(args);
87 }
88
89 void logWarning(const char* fmt, ...)
90 {
91 va_list args;
92 va_start(args, fmt);
93
94 printLog_(LOG_WARNING, fmt, args);
95
96 va_end(args);
97 }
98
99 void logInfo(const char* fmt, ...)
100 {
101 va_list args;
102 va_start(args, fmt);
103
104 printLog_(LOG_INFO, fmt, args);
105
106 va_end(args);
107 }
108
109 void logDebug(const char* fmt, ...)
110 {
111 va_list args;
112 va_start(args, fmt);
113
114 printLog_(LOG_DEBUG, fmt, args);
115
116 va_end(args);
117 }
118
119 static void logScript(const char* fmt, ...)
120 {
121 va_list args;
122 va_start(args, fmt);
123
124 printLog_(LOG_SCRIPT, fmt, args);
125
126 va_end(args);
127 }
128
129 int logScript(Script& script)
130 {
131 Script::Value param = script[1];
132
133 while (!param.isNone())
134 {
135 if (param.isString())
136 {
137 std::string str;
138 param.get(str);
139 logScript("%s", str.c_str());
140 }
141 else if (param.isBoolean())
142 {
143 if (param) logScript("true");
144 else logScript("false");
145
146 }
147 else if (param.isNil())
148 {
149 logScript("nil");
150 }
151 else
152 {
153 logScript("%s (%X)", param.getTypeName().c_str(),
154 param.getIdentifier());
155 }
156
157 param.index++;
158 }
159
160 return 0;
161 }
162
163
164 void importLogScript(Script& script)
165 {
166 script.importFunction("print", (int (*)(Script&))logScript);
167 }
168
169
170 } // namespace Mf
171
This page took 0.035439 seconds and 4 git commands to generate.