Changeset 42

User picture

Author: tyr377

(2011/08/19 04:48) 9 months ago

logging functionality for raw data implemented (printing a hex dump)

Affected files

Updated CameraControl/trunk/include/Log.h Download diff

4142
15
15
16
public:
16
public:
17
	static void print(LogLevel level, const char* file, int line, ...);
17
	static void print(LogLevel level, const char* file, int line, ...);
18
	static void printHexDump(const char* file, int line, unsigned char* buffer, size_t len);
18
	
19
	
19
	static void     setLogLevel(LogLevel level);
20
	static void     setLogLevel(LogLevel level);
20
	static LogLevel getLogLevel();
21
	static LogLevel getLogLevel();
21
	
22
	
22
	static void setLogFile(const char* filename);
23
	static void setLogFile(const char* filename);
23
24
24
25
26
private: // member
25
private: // member
27
	static Log* _self;
26
	static Log* _self;
28
	LogLevel    _level;
27
	LogLevel    _level;
...
...
34
#define LOG_ERROR(...) Log::print(ERROR,  __FILE__, __LINE__, ##__VA_ARGS__)
33
#define LOG_ERROR(...) Log::print(ERROR,  __FILE__, __LINE__, ##__VA_ARGS__)
35
#define LOG_WARN (...) Log::print(WARNING,__FILE__, __LINE__, ##__VA_ARGS__)
34
#define LOG_WARN (...) Log::print(WARNING,__FILE__, __LINE__, ##__VA_ARGS__)
36
#define LOG_DEBUG(...) Log::print(DEBUG,  __FILE__, __LINE__, ##__VA_ARGS__)
35
#define LOG_DEBUG(...) Log::print(DEBUG,  __FILE__, __LINE__, ##__VA_ARGS__)
36
#define LOG_HEX(buffer, len)   Log::printHexDump( __FILE__, __LINE__, buffer, len)

Updated CameraControl/trunk/src/Log.cpp Download diff

4142
1
#include "Log.h"
1
#include "Log.h"
2
#include <stdio.h>
2
#include <stdarg.h>
3
#include <stdarg.h>
3
#include <string>
4
#include <string>
4
#include <sstream>
5
#include <sstream>
...
...
19
	return "";
20
	return "";
20
 }
21
 }
21
22
23
 static FILE* getStream(LogLevel level) {
24
	if (level == ERROR)
25
		return stderr;
26
27
	return stdout;
28
}
29
30
static std::string getTimestamp() {
31
	struct _timeb timebuffer;
32
	_ftime_s( &timebuffer );
33
34
	struct tm* time = gmtime(&(timebuffer.time));
35
	const char* timeformat = "%Y-%m-%dT%H:%M:%S";	// TODO: output with milliseconds would be nice
36
													
37
	char t[32];
38
	strftime(t, 32, timeformat, time);					// TODO: time is UTC based, time zone based would be nice
39
40
	return t;
41
}
22
Log::Log()
42
Log::Log()
23
	: _level(DEBUG)
43
	: _level(DEBUG)
24
	, _logFD(NULL)
44
	, _logFD(NULL)
...
...
35
	if (getInstance()->_level == OFF)
55
	if (getInstance()->_level == OFF)
36
		return;
56
		return;
37
57
38
	struct _timeb timebuffer;
39
	_ftime_s( &timebuffer );
40
41
	struct tm* time = gmtime(&(timebuffer.time));
42
	const char* timeformat = "%Y-%m-%dT%H:%M:%S";	// TODO: output with milliseconds would be nice
43
													
44
	char t[20];
45
	_locale_t t;
46
	strftime(t,20,timeformat,time);					// TODO: time is UTC based, time zone based would be nice
47
	
48
	va_list argptr;
58
	va_list argptr;
49
	va_start(argptr, line);
59
	va_start(argptr, line);
50
60
51
	char* format = va_arg(argptr,char*);
61
	char* format = va_arg(argptr,char*);
52
62
53
	std::stringstream f;
63
	std::stringstream f;
54
	f << t << " " << LogLevelToString(level) << " (" << file << ":" << line << ") : ";
64
	f << getTimestamp() << " " << LogLevelToString(level) << " (" << file << ":" << line << ") : ";
55
	
65
	
56
	if (format) {
66
	char* buffer = NULL;
67
	int size = 0;
68
	if (format)
57
		f << format;
69
		f << format;
58
		vprintf(f.str().c_str(), argptr);
70
	
59
	} else
71
	f << "\n";
60
		printf(f.str().c_str());
61
72
73
	size = vsnprintf(NULL,0,f.str().c_str(), argptr);
74
	buffer = new char[size+1];	
75
	vsnprintf(buffer,size+1,f.str().c_str(), argptr);
76
77
	if (level <= _self->_level)
78
		fprintf(getStream(level), buffer);
79
80
	if (_self->_logFD)
81
		fprintf(_self->_logFD, buffer);
82
	
83
	delete [] buffer;
62
}
84
}
63
85
86
void Log::printHexDump(const char* file, int line, unsigned char* buffer, size_t len) {
87
	if (getInstance()->_level != DEBUG)
88
		return;
89
	
90
	std::stringstream msg;
91
	
92
	msg << getTimestamp() << " " << LogLevelToString(DEBUG) << " (" << file << ":" << line
<< ") :\n";
93
	
94
	char tmp[16];
95
	for (size_t n = 0; n < len; ++n) {
96
		
97
		if (n%8 == 0 && n%16 > 0)
98
			msg << " ";
99
		if (n%16 == 0 ) {
100
			if (n > 0) 
101
				msg << "\n";
102
			sprintf(tmp,"%08X  ", n);
103
			msg << tmp;
104
		}
105
		
106
		sprintf(tmp,"%02X ", buffer[n]);
107
		msg << tmp;
108
109
	}
110
111
	fprintf(getStream(DEBUG), msg.str().c_str());
112
113
	if (_self->_logFD)
114
		fprintf(_self->_logFD, msg.str().c_str());
115
}
116
64
void Log::setLogLevel(LogLevel level) {
117
void Log::setLogLevel(LogLevel level) {
65
	getInstance()->_level = level;
118
	getInstance()->_level = level;
66
}
119
}

Updated CameraControl/trunk/testCameraControl/main.cpp Download diff

4142
5
5
6
6
7
int main(int argc, char** argv) {
7
int main(int argc, char** argv) {
8
	LOG_DEBUG();
8
	LOG_DEBUG("hier");
9
9
10
	unsigned char t[256];
11
	for (int n = 0; n<256; ++n)	t[n] = (unsigned char) n;
12
	LOG_HEX(t,256);
13
10
	return EXIT_SUCCESS;
14
	return EXIT_SUCCESS;
11
}
15
}
12
16