Commit | Line | Data |
---|---|---|
9c66c9ff JM |
1 | #include <stdio.h> |
2 | #include <stdarg.h> | |
3 | #include <time.h> | |
4 | #include <sys/time.h> | |
5 | ||
6 | #include "log.h" | |
7 | ||
8 | int debug = 0; | |
9 | ||
e23fe653 JM |
10 | #ifdef _WIN32 |
11 | #include <windows.h> | |
12 | #include <fcntl.h> | |
13 | #include <io.h> | |
14 | ||
15 | static const WORD MAX_CONSOLE_LINES = 500; | |
16 | ||
9c66c9ff JM |
17 | void logit(int type, const char * format, ...) |
18 | { | |
e23fe653 JM |
19 | static int hConHandle = 0; |
20 | ||
21 | if (hConHandle == 0) { | |
22 | CONSOLE_SCREEN_BUFFER_INFO coninfo; | |
23 | AllocConsole(); | |
24 | //AttachConsole(ATTACH_PARENT_PROCESS); | |
25 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); | |
26 | coninfo.dwSize.Y = MAX_CONSOLE_LINES; | |
27 | SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); | |
28 | ||
29 | int hConHandle; | |
30 | long lStdHandle; | |
31 | FILE *fp; | |
32 | ||
33 | // redirect unbuffered STDOUT to the console | |
34 | lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); | |
35 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
36 | fp = _fdopen( hConHandle, "w" ); | |
37 | *stdout = *fp; | |
38 | setvbuf( stdout, NULL, _IONBF, 0 ); | |
39 | ||
40 | // redirect unbuffered STDIN to the console | |
41 | lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); | |
42 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
43 | fp = _fdopen( hConHandle, "r" ); | |
44 | *stdin = *fp; | |
45 | ||
46 | setvbuf( stdin, NULL, _IONBF, 0 ); | |
47 | ||
48 | // redirect unbuffered STDERR to the console | |
49 | lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); | |
50 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
51 | fp = _fdopen( hConHandle, "w" ); | |
52 | *stderr = *fp; | |
53 | ||
54 | setvbuf( stderr, NULL, _IONBF, 0 ); | |
55 | } | |
56 | ||
9c66c9ff JM |
57 | va_list va; |
58 | va_start(va, format); | |
59 | ||
60 | struct timeval now; | |
61 | gettimeofday(&now, NULL); | |
62 | ||
63 | struct tm * now_tm = localtime(&(now.tv_sec)); | |
64 | ||
65 | if (type == LOG_INFO) { | |
66 | vfprintf(stdout, format, va); | |
67 | fprintf(stdout, "\n"); | |
68 | } else | |
69 | if (type == LOG_DEBUG) { | |
70 | if (debug > 0) { | |
71 | fprintf(stderr, "%02d:%02d:%02d.%03d ", now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec, (int)(now.tv_usec / 1000)); | |
72 | vfprintf(stderr, format, va); | |
73 | fprintf(stderr, "\n"); | |
74 | } | |
75 | } else { | |
76 | vfprintf(stderr, format, va); | |
77 | fprintf(stderr, "\n"); | |
78 | } | |
79 | ||
80 | va_end(va); | |
81 | } | |
e23fe653 JM |
82 | |
83 | #else | |
84 | ||
85 | void logit(int type, const char * format, ...) | |
86 | { | |
87 | va_list va; | |
88 | va_start(va, format); | |
89 | ||
90 | struct timeval now; | |
91 | gettimeofday(&now, NULL); | |
92 | ||
93 | struct tm * now_tm = localtime(&(now.tv_sec)); | |
94 | ||
95 | if (type == LOG_INFO) { | |
96 | vfprintf(stdout, format, va); | |
97 | fprintf(stdout, "\n"); | |
98 | } else | |
99 | if (type == LOG_DEBUG) { | |
100 | if (debug > 0) { | |
101 | fprintf(stderr, "%02d:%02d:%02d.%03d ", now_tm->tm_hour, now_tm->tm_min, now_tm->tm_sec, (int)(now.tv_usec / 1000)); | |
102 | vfprintf(stderr, format, va); | |
103 | fprintf(stderr, "\n"); | |
104 | } | |
105 | } else { | |
106 | vfprintf(stderr, format, va); | |
107 | fprintf(stderr, "\n"); | |
108 | } | |
109 | ||
110 | va_end(va); | |
111 | } | |
112 | ||
113 | #endif |