1 /*
2 Copyright (c) 2009 Andrew Caudwell (acaudwell@gmail.com)
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "logger.h"
29 #include <boost/assign/list_of.hpp>
30
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34
35 std::map<int,std::string> log_levels = boost::assign::map_list_of
36 (LOG_LEVEL_ERROR, " ERROR" )
37 (LOG_LEVEL_CONSOLE, "CONSOLE" )
38 (LOG_LEVEL_INFO, " INFO" )
39 (LOG_LEVEL_SCRIPT, " SCRIPT" )
40 (LOG_LEVEL_DEBUG, " DEBUG" )
41 (LOG_LEVEL_WARN, " WARN" )
42 (LOG_LEVEL_PEDANTIC, " PEDANT" );
43
44 #define PARSE_AND_LOG(LOG_LEVEL) \
45 Logger* logger = Logger::getDefault(); \
46 \
47 if(!logger || logger->getLevel() < LOG_LEVEL) return; \
48 \
49 char msgbuff[65536]; \
50 char* buffer = msgbuff; \
51 \
52 va_list vl; \
53 \
54 va_start(vl, str); \
55 int string_size = vsnprintf(buffer, sizeof(msgbuff), str, vl) + 1; \
56 \
57 if(string_size > sizeof(msgbuff)) { \
58 buffer = new char[string_size]; \
59 string_size = vsnprintf(buffer, string_size, str, vl); \
60 } \
61 \
62 va_end(vl); \
63 \
64 \
65 logger->message( LOG_LEVEL, buffer ); \
66 \
67 if(buffer != msgbuff) delete[] buffer;
68
69 // LoggerMessage
70
LoggerMessage(int level,const std::string & message)71 LoggerMessage::LoggerMessage(int level, const std::string& message)
72 : level(level), message(message) {
73 }
74
75 // Logger
76
getDefault()77 Logger* Logger::getDefault() {
78 return default_logger;
79 }
80
Logger(int level,FILE * stream,int hist_capacity)81 Logger::Logger(int level, FILE* stream, int hist_capacity) {
82 init(level, stream, hist_capacity);
83 }
84
init(int level,FILE * stream,int hist_capacity)85 void Logger::init(int level, FILE* stream, int hist_capacity) {
86 this->level = level;
87 this->stream = stream;
88 this->hist_capacity = hist_capacity;
89 this->message_count = 0;
90 }
91
getMessageCount()92 int Logger::getMessageCount() {
93 return message_count;
94 }
95
message(int level,const std::string & message)96 void Logger::message(int level, const std::string& message) {
97
98 if(!level || this->level < level) return;
99
100 if(stream != 0) {
101 fprintf(stream, "%s: %s\n", log_levels[level].c_str(), message.c_str());
102 }
103
104 if(!hist_capacity) return;
105
106 while(history.size() >= hist_capacity) {
107 history.pop_front();
108 }
109
110 history.push_back(LoggerMessage(level, message));
111 message_count++;
112 }
113
getHistory() const114 const std::deque<LoggerMessage>& Logger::getHistory() const {
115 return history;
116 }
117
setHistoryCapacity(int hist_capacity)118 void Logger::setHistoryCapacity(int hist_capacity) {
119 this->hist_capacity = hist_capacity;
120 }
121
warnLog(const char * str,...)122 void warnLog(const char *str, ...) {
123 PARSE_AND_LOG(LOG_LEVEL_WARN);
124 }
125
debugLog(const char * str,...)126 void debugLog(const char *str, ...) {
127 PARSE_AND_LOG(LOG_LEVEL_DEBUG);
128 }
129
infoLog(const char * str,...)130 void infoLog(const char *str, ...) {
131 PARSE_AND_LOG(LOG_LEVEL_INFO);
132 }
133
errorLog(const char * str,...)134 void errorLog(const char *str, ...) {
135 PARSE_AND_LOG(LOG_LEVEL_ERROR)
136 }
137
consoleLog(const char * str,...)138 void consoleLog(const char *str, ...) {
139 PARSE_AND_LOG(LOG_LEVEL_CONSOLE);
140 }
141
scriptLog(const char * str,...)142 void scriptLog(const char *str, ...) {
143 PARSE_AND_LOG(LOG_LEVEL_SCRIPT);
144 }
145
pedanticLog(const char * str,...)146 void pedanticLog(const char *str, ...) {
147 PARSE_AND_LOG(LOG_LEVEL_PEDANTIC);
148 }
149
150 Logger* Logger::default_logger = new Logger(LOG_LEVEL_ERROR, stderr, 0);
151