summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/include/Nanorex/Utility/NXLogger.h
blob: cb0f501248c6225ff7a69866366444c20a4f28e6 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#ifndef NX_LOGGER_H
#define NX_LOGGER_H

#include <stdio.h>

#include <list>
#include <string>

#include <QMutex>
#include <QDateTime>
//#include <QMutexLocker>

namespace Nanorex {

typedef enum NXLogLevel {
	NXLogLevel_Debug,	// General debugging.
	NXLogLevel_Config,	// Debugging configurations (graphics depth, os, etc.)
	NXLogLevel_Info,	// Information interesting to the user.
	NXLogLevel_Warning,	// Important things the user should know.
	NXLogLevel_Severe	// Things that should stop the user before continuing.
};

static char LogLevelNames[][8] = {
	"DEBUG", "CONFIG", "INFO", "WARNING", "SEVERE"
};


/* CLASS: LogRecord */
/**
 * Used internally by NXLogHandler and NXLogger.
 */
class LogRecord {
	public:
		LogRecord(NXLogLevel logLevel, const std::string& source,
				  const std::string& message) {
			this->logLevel = logLevel;
			this->source = source;
			this->message = message;
			dateTime = QDateTime::currentDateTime();
		}
		
		NXLogLevel getLogLevel() { return logLevel; }
		QDateTime getDateTime() { return dateTime; }
		std::string getSource() { return source; }
		std::string getMessage() { return message; }
		
	private:
		NXLogLevel logLevel;
		QDateTime dateTime;
		std::string source;
		std::string message;
};


/* CLASS: NXLogHandler */
/**
 * Base class for NXLogger log entry emission handlers.
 * @ingroup NanorexUtility, Logging
 */
class NXLogHandler {
	public:
		NXLogHandler(NXLogLevel logLevel) { this->logLevel = logLevel; }
		virtual ~NXLogHandler() { }
		
		virtual void publish(LogRecord logRecord) = 0;
	
	protected:
		NXLogLevel logLevel;
};


/* CLASS: NXLogger */
/**
 * A simple logging mechanism.
 * @ingroup NanorexUtility, Logging
 */
class NXLogger {
	public:
		NXLogger() { ThisInstance = this; }
		~NXLogger() {
			log(NXLogLevel_Info,
				"NXLogger",
				"*********** Log End *************");
			
			// Delete handlers
			std::list<NXLogHandler*>::iterator iter = logHandlers.begin();
			while (iter != logHandlers.end()) {
				delete (*iter);
				iter++;
			}
		}
		
		void log(NXLogLevel logLevel, const std::string& source,
				 const std::string& message) {
			mutex.lock();
			LogRecord logRecord(logLevel, source, message);
			std::list<NXLogHandler*>::iterator iter = logHandlers.begin();
			while (iter != logHandlers.end()) {
				(*iter)->publish(logRecord);
				iter++;
			}
			mutex.unlock();
		}
		
		void addHandler(NXLogHandler* logHandler) {
			logHandlers.push_back(logHandler);
			logHandler->publish(LogRecord(NXLogLevel_Info,
										 "NXLogger",
										 "*********** Log Start ***********"));
		}
		
		static NXLogger* Instance() { return ThisInstance; }

	private:
		QMutex mutex;
		std::list<NXLogHandler*> logHandlers;
		static NXLogger* ThisInstance;
};


// Convenience macros
#define NXLOG_DEBUG(source, message) { \
	Nanorex::NXLogger* logger = Nanorex::NXLogger::Instance(); \
	if (logger != 0) \
		logger->log(Nanorex::NXLogLevel_Debug, source, message); \
};

#define NXLOG_CONFIG(source, message) { \
	Nanorex::NXLogger* logger = Nanorex::NXLogger::Instance(); \
	if (logger != 0) \
		logger->log(Nanorex::NXLogLevel_Config, source, message); \
};

#define NXLOG_INFO(source, message) { \
	Nanorex::NXLogger* logger = Nanorex::NXLogger::Instance(); \
	if (logger != 0) \
    logger->log(Nanorex::NXLogLevel_Info, (source), (message)); \
};

#define NXLOG_WARNING(source, message) { \
	Nanorex::NXLogger* logger = Nanorex::NXLogger::Instance(); \
	if (logger != 0) \
		logger->log(Nanorex::NXLogLevel_Warning, source, message); \
};

#define NXLOG_SEVERE(source, message) { \
	Nanorex::NXLogger* logger = Nanorex::NXLogger::Instance(); \
	if (logger != 0) \
		logger->log(Nanorex::NXLogLevel_Severe, source, message); \
};


/* CLASS: NXConsoleLogHandler */
/**
 * Emits log entries to the console.
 * @ingroup NanorexUtility, Logging
 */
class NXConsoleLogHandler : public NXLogHandler {
	public:
		NXConsoleLogHandler(NXLogLevel logLevel) : NXLogHandler(logLevel) { }
		void publish(LogRecord logRecord) {
			if (logRecord.getLogLevel() >= logLevel) {
				//mutex.lock();
				printf("%s  [%-7s]  %s %s\n",
					   qPrintable(logRecord.getDateTime()
									.toString("yyyy-MM-dd hh:mm:ss.zzz")),
					   LogLevelNames[logRecord.getLogLevel()],
					   (logRecord.getSource().length() == 0 ?
							"" : logRecord.getSource().append(":").c_str()),
					   logRecord.getMessage().c_str());
				fflush(0);
				//mutex.unlock();
			}
		}

	private:
		QMutex mutex;
};


/* CLASS: NXFileLogHandler */
/**
 * Emits log entries to a specified file.
 * @ingroup NanorexUtility, Logging
 */
class NXFileLogHandler : public NXLogHandler {
	public:
		NXFileLogHandler(const std::string& filename, NXLogLevel logLevel) :
				NXLogHandler(logLevel) {
			filehandle = fopen(filename.c_str(), "w");
			if (filehandle != 0) {
				fprintf(filehandle,
						"%s  NXFileLogHandler started. Writing to: %s\n",
						qPrintable(QDateTime::currentDateTime()
							.toString("yyyy-MM-dd hh:mm:ss.zzz")),
						filename.c_str());
				printf("NXFileLogHandler started. Writing to: %s\n",
					   filename.c_str());
			} else
				printf("NXFileLogHandler could not open file for writing: %s\n",
					   filename.c_str());
		}
		~NXFileLogHandler() {
			if (filehandle != 0)
				fclose(filehandle);
		}
		void publish(LogRecord logRecord) {
			if ((filehandle != 0) && (logRecord.getLogLevel() >= logLevel)) {
				//mutex.lock();
				fprintf(filehandle,"%s  [%-7s]  %s %s\n",
						qPrintable(logRecord.getDateTime()
							.toString("yyyy-MM-dd hh:mm:ss.zzz")),
						LogLevelNames[logRecord.getLogLevel()],
						(logRecord.getSource().length() == 0 ?
							"" : logRecord.getSource().append(":").c_str()),
						logRecord.getMessage().c_str());
				//mutex.unlock();
			}
		}

	private:
		QMutex mutex;
		FILE* filehandle;
};

} // Nanorex::

#endif