Mercurial > hg > nnchat
comparison main.c @ 511:291e3caa91a0
Move logParseFilename() to nn_log_parse_filename() under util.c and rename logFileOpen() and logFileClose().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 05 Jun 2012 19:28:34 +0300 |
parents | 942eea564b15 |
children | 93c8ba1ef55f |
comparison
equal
deleted
inserted
replaced
510:54f4f84bae5c | 511:291e3caa91a0 |
---|---|
1258 | 1258 |
1259 return FALSE; | 1259 return FALSE; |
1260 } | 1260 } |
1261 | 1261 |
1262 | 1262 |
1263 #define VPUTCH(CH) th_vputch(&bufData, &bufSize, &bufLen, CH) | 1263 BOOL nn_log_file_open(void) |
1264 #define VPUTS(STR) th_vputs(&bufData, &bufSize, &bufLen, STR) | |
1265 | |
1266 char *logParseFilename(const char *fmt, int id) | |
1267 { | |
1268 size_t bufSize = strlen(fmt) + TH_BUFGROW, bufLen = 0; | |
1269 char *bufData = th_malloc(bufSize); | |
1270 char tmpBuf[32]; | |
1271 const char *s = fmt; | |
1272 | |
1273 while (*s) | |
1274 { | |
1275 if (*s == '%') | |
1276 { | |
1277 s++; | |
1278 switch (*s) | |
1279 { | |
1280 case 'i': | |
1281 snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id); | |
1282 VPUTS(tmpBuf); | |
1283 break; | |
1284 | |
1285 case 'd': | |
1286 snprintf(tmpBuf, sizeof(tmpBuf), "%d", id); | |
1287 VPUTS(tmpBuf); | |
1288 break; | |
1289 | |
1290 case '%': | |
1291 VPUTCH('%'); | |
1292 break; | |
1293 } | |
1294 s++; | |
1295 } | |
1296 else | |
1297 { | |
1298 VPUTCH(*s); | |
1299 s++; | |
1300 } | |
1301 } | |
1302 | |
1303 VPUTCH(0); | |
1304 return bufData; | |
1305 } | |
1306 | |
1307 | |
1308 BOOL logFileOpen(void) | |
1309 { | 1264 { |
1310 char *filename; | 1265 char *filename; |
1311 | 1266 |
1312 if (optLogFilename == NULL || !optLogEnable) | 1267 if (optLogFilename == NULL || !optLogEnable) |
1313 return FALSE; | 1268 return FALSE; |
1314 | 1269 |
1315 filename = logParseFilename(optLogFilename, optPort); | 1270 filename = nn_log_parse_filename(optLogFilename, optPort); |
1316 | 1271 |
1317 if ((optLogFile = fopen(filename, "a")) == NULL) | 1272 if ((optLogFile = fopen(filename, "a")) == NULL) |
1318 { | 1273 { |
1319 errorMsg("Could not open logfile '%s' for appending!\n", filename); | 1274 errorMsg("Could not open logfile '%s' for appending!\n", filename); |
1320 th_free(filename); | 1275 th_free(filename); |
1325 | 1280 |
1326 return TRUE; | 1281 return TRUE; |
1327 } | 1282 } |
1328 | 1283 |
1329 | 1284 |
1330 void logFileClose(void) | 1285 void nn_log_file_close(void) |
1331 { | 1286 { |
1332 if (optLogFile) | 1287 if (optLogFile) |
1333 { | 1288 { |
1334 fclose(optLogFile); | 1289 fclose(optLogFile); |
1335 optLogFile = NULL; | 1290 optLogFile = NULL; |
1469 { | 1424 { |
1470 th_llist_append(&setIdleMessages, th_strdup(".")); | 1425 th_llist_append(&setIdleMessages, th_strdup(".")); |
1471 } | 1426 } |
1472 | 1427 |
1473 // Open logfile | 1428 // Open logfile |
1474 logFileOpen(); | 1429 nn_log_file_open(); |
1475 | 1430 |
1476 // Initialize network | 1431 // Initialize network |
1477 if (!nn_network_init()) | 1432 if (!nn_network_init()) |
1478 { | 1433 { |
1479 THERR("Could not initialize network subsystem.\n"); | 1434 THERR("Could not initialize network subsystem.\n"); |
1986 nn_conn_close(conn); | 1941 nn_conn_close(conn); |
1987 nn_network_close(); | 1942 nn_network_close(); |
1988 | 1943 |
1989 THMSG(1, "Connection terminated.\n"); | 1944 THMSG(1, "Connection terminated.\n"); |
1990 | 1945 |
1991 logFileClose(); | 1946 nn_log_file_close(); |
1992 | 1947 |
1993 return 0; | 1948 return 0; |
1994 } | 1949 } |