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