Mercurial > hg > nnchat
comparison nnchat.c @ 156:0a9fe14882dc
Introduce utility function getTimeStamp() and use it to lessen code duplication.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 03 Nov 2010 12:26:10 +0200 |
parents | 0720ca51673e |
children | d3c78f2ef77b |
comparison
equal
deleted
inserted
replaced
155:0720ca51673e | 156:0a9fe14882dc |
---|---|
147 } | 147 } |
148 | 148 |
149 return TRUE; | 149 return TRUE; |
150 } | 150 } |
151 | 151 |
152 BOOL getTimeStamp(char *str, size_t len, const char *fmt) | |
153 { | |
154 time_t stamp = time(NULL); | |
155 struct tm *stamp_tm; | |
156 if ((stamp_tm = localtime(&stamp)) != NULL) { | |
157 strftime(str, len, fmt, stamp_tm); | |
158 return TRUE; | |
159 } else { | |
160 str[0] = 0; | |
161 return FALSE; | |
162 } | |
163 } | |
152 | 164 |
153 void updateStatus(BOOL insertMode) | 165 void updateStatus(BOOL insertMode) |
154 { | 166 { |
155 char tmpStr[128] = ""; | 167 char tmpStr[128]; |
156 time_t timeStamp; | |
157 struct tm *tmpTime;; | |
158 | 168 |
159 if (statusWin == NULL) return; | 169 if (statusWin == NULL) return; |
160 | 170 |
161 timeStamp = time(NULL); | 171 getTimeStamp(tmpStr, sizeof(tmpStr), "%H:%M:%S"); |
162 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
163 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime); | |
164 } | |
165 | 172 |
166 wbkgdset(statusWin, COLOR_PAIR(10)); | 173 wbkgdset(statusWin, COLOR_PAIR(10)); |
167 werase(statusWin); | 174 werase(statusWin); |
168 | 175 |
169 wattrset(statusWin, A_BOLD | COLOR_PAIR(11)); | 176 wattrset(statusWin, A_BOLD | COLOR_PAIR(11)); |
283 } | 290 } |
284 | 291 |
285 | 292 |
286 void printMsgV(BOOL addStamp, BOOL logOnly, const char *fmt, va_list ap) | 293 void printMsgV(BOOL addStamp, BOOL logOnly, const char *fmt, va_list ap) |
287 { | 294 { |
288 char tmpStr[128] = "", buf[8192]; | 295 char tmpStr[128], buf[8192]; |
289 time_t timeStamp; | 296 |
290 struct tm *tmpTime;; | 297 getTimeStamp(tmpStr, sizeof(tmpStr), "˝17˝[˝11˝%H:%M:%S˝17˝]˝0˝ "); |
291 | |
292 timeStamp = time(NULL); | |
293 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
294 strftime(tmpStr, sizeof(tmpStr), "˝17˝[˝11˝%H:%M:%S˝17˝]˝0˝ ", tmpTime); | |
295 } | |
296 | 298 |
297 vsnprintf(buf, sizeof(buf), fmt, ap); | 299 vsnprintf(buf, sizeof(buf), fmt, ap); |
298 | 300 |
299 if (optLogFile) { | 301 if (optLogFile) { |
300 if (addStamp) printFile(optLogFile, tmpStr); | 302 if (addStamp) printFile(optLogFile, tmpStr); |
419 } | 421 } |
420 | 422 |
421 | 423 |
422 int handleLogin(int sock, const char *str) | 424 int handleLogin(int sock, const char *str) |
423 { | 425 { |
424 char tmpStr[256] = ""; | 426 char tmpStr[256]; |
425 time_t timeStamp; | 427 |
426 struct tm *tmpTime;; | 428 getTimeStamp(tmpStr, sizeof(tmpStr), "%c"); |
427 | |
428 timeStamp = time(NULL); | |
429 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
430 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime); | |
431 } | |
432 | 429 |
433 if (!strncmp(str, "FAILURE", 7)) { | 430 if (!strncmp(str, "FAILURE", 7)) { |
434 printMsg("˝1˝Login failure˝0˝ - ˝3˝%s˝0˝\n", tmpStr); | 431 printMsg("˝1˝Login failure˝0˝ - ˝3˝%s˝0˝\n", tmpStr); |
435 return -2; | 432 return -2; |
436 } else if (!strncmp(str, "SUCCESS", 7)) { | 433 } else if (!strncmp(str, "SUCCESS", 7)) { |