0639a601dba5f2edb4e64212eba3862425e8873e
[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
35
36 namespace Mf {
37
38
39 static LogLevel logLevel_ = LOG_WARNING;
40
41 inline void printLog_(int logLevel, const char* fmt, va_list args)
42 {
43 if (logLevel_ < logLevel) return;
44
45 switch (logLevel)
46 {
47 case LOG_ERROR:
48 fprintf(stderr, " error: ");
49 break;
50 case LOG_WARNING:
51 fprintf(stderr, "warning: ");
52 break;
53 case LOG_INFO:
54 fprintf(stderr, " info: ");
55 break;
56 case LOG_DEBUG:
57 fprintf(stderr, " debug: ");
58 break;
59 }
60
61 vfprintf(stderr, fmt, args);
62 fprintf(stderr, "\n");
63 }
64
65
66 LogLevel setLogLevel(LogLevel level)
67 {
68 if (level != 0)
69 logLevel_ = level;
70
71 return logLevel_;
72 }
73
74
75 void logError(const char* fmt, ...)
76 {
77 va_list args;
78 va_start(args, fmt);
79
80 printLog_(LOG_ERROR, fmt, args);
81
82 va_end(args);
83 }
84
85 void logWarning(const char* fmt, ...)
86 {
87 va_list args;
88 va_start(args, fmt);
89
90 printLog_(LOG_WARNING, fmt, args);
91
92 va_end(args);
93 }
94
95 void logInfo(const char* fmt, ...)
96 {
97 va_list args;
98 va_start(args, fmt);
99
100 printLog_(LOG_INFO, fmt, args);
101
102 va_end(args);
103 }
104
105 void logDebug(const char* fmt, ...)
106 {
107 va_list args;
108 va_start(args, fmt);
109
110 printLog_(LOG_DEBUG, fmt, args);
111
112 va_end(args);
113 }
114
115
116 } // namespace Mf
117
This page took 0.037652 seconds and 3 git commands to generate.