comparison nnchat.c @ 242:cb86d7543be2

Add functionality for generating logfile names based on port etc. and configuration option for logfile name pattern and enabling logging.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 20 Apr 2011 18:48:42 +0300
parents e85d453e82eb
children fce4e2e31d69
comparison
equal deleted inserted replaced
241:a1ee6c76ca1c 242:cb86d7543be2
64 *statusWin = NULL, 64 *statusWin = NULL,
65 *editWin = NULL; 65 *editWin = NULL;
66 BOOL setPrvMode = FALSE; 66 BOOL setPrvMode = FALSE;
67 BOOL setIgnoreMode = FALSE; 67 BOOL setIgnoreMode = FALSE;
68 BOOL optDebug = FALSE; 68 BOOL optDebug = FALSE;
69 69 BOOL optLogEnable = FALSE;
70 70
71 qlist_t *nnIgnoreList = NULL; 71 qlist_t *nnIgnoreList = NULL;
72 nn_userhash_t *nnUsers = NULL; 72 nn_userhash_t *nnUsers = NULL;
73 char *setConfigFile = NULL, 73 char *setConfigFile = NULL,
74 *setBrowser = NULL; 74 *setBrowser = NULL;
128 THMSG(1, "Using color #%06x\n", optUserColor); 128 THMSG(1, "Using color #%06x\n", optUserColor);
129 break; 129 break;
130 130
131 case 5: 131 case 5:
132 optLogFilename = optArg; 132 optLogFilename = optArg;
133 optLogEnable = TRUE;
133 break; 134 break;
134 135
135 case 7: 136 case 7:
136 optSite = optArg; 137 optSite = optArg;
137 break; 138 break;
939 } 940 }
940 941
941 return FALSE; 942 return FALSE;
942 } 943 }
943 944
945 #define VPUTCH(CH) th_vputch(&bufData, &bufSize, &bufLen, CH)
946 #define VPUTS(STR) th_vputs(&bufData, &bufSize, &bufLen, STR)
947
944 char *logParseFilename(const char *fmt, int id) 948 char *logParseFilename(const char *fmt, int id)
945 { 949 {
946 size_t tmpLen = strlen(fmt) + 32; 950 size_t bufSize = strlen(fmt) + 32, bufLen = 0;
947 char *res = NULL, tmpBuf[32]; 951 char *bufData = NULL, tmpBuf[32];
948 const char *s = fmt; 952 const char *s = fmt;
949 953
950 if ((res = th_malloc(len)) == NULL)
951 return NULL;
952
953 while (*s) { 954 while (*s) {
954 if (*s == '%') { 955 if (*s == '%') {
955 s++; 956 s++;
956 switch (*s) { 957 switch (*s) {
957 case 'i': 958 case 'i':
958
959 {
960 snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id); 959 snprintf(tmpBuf, sizeof(tmpBuf), "%05d", id);
961 len -= strlen(tmpBuf) + 1;
962 if (len <= 0) {
963 res = th_realloc(res,
964 }
965 VPUTS(tmpBuf); 960 VPUTS(tmpBuf);
966 }
967 break; 961 break;
968 962
969 case '%': 963 case '%':
970 VPUTCH(res, '%'); 964 VPUTCH('%');
971 break; 965 break;
972 966
973 default: 967 default:
974 VPUTCH(res, '%'); 968 VPUTCH('%');
975 VPUTCH(res, *s); 969 VPUTCH(*s);
976 break; 970 break;
977 } 971 }
978 } 972 s++;
979 } 973 } else {
974 VPUTCH(*s);
975 s++;
976 }
977 }
978
979 VPUTCH(0);
980 return bufData;
980 } 981 }
981 982
982 983
983 BOOL logFileOpen(void) 984 BOOL logFileOpen(void)
984 { 985 {
985 char *filename; 986 char *filename;
986 987
987 if (optLogFilename == NULL) 988 if (optLogFilename == NULL || !optLogEnable)
988 return FALSE; 989 return FALSE;
989 990
990 991 filename = logParseFilename(optLogFilename, optPort);
991 992
992 if ((optLogFile = fopen(filename, "a")) == NULL) { 993 if ((optLogFile = fopen(filename, "a")) == NULL) {
993 errMsg("Could not open logfile '%s' for appending!\n", filename); 994 errorMsg("Could not open logfile '%s' for appending!\n", filename);
994 th_free(filename); 995 th_free(filename);
995 return FALSE; 996 return FALSE;
996 } 997 }
998
999 th_free(filename);
1000
997 return TRUE; 1001 return TRUE;
998 } 1002 }
999 1003
1000 BOOL logFileClose(void) 1004 void logFileClose(void)
1001 { 1005 {
1006 if (optLogFile) {
1007 fclose(optLogFile);
1008 optLogFile = NULL;
1009 }
1002 } 1010 }
1003 1011
1004 char *promptRequester(const char *info, BOOL allowEmpty) 1012 char *promptRequester(const char *info, BOOL allowEmpty)
1005 { 1013 {
1006 char tmpBuf[512], *ptr; 1014 char tmpBuf[512], *ptr;
1071 th_cfg_add_int(&tmpcfg, "port", &optPort, optPort); 1079 th_cfg_add_int(&tmpcfg, "port", &optPort, optPort);
1072 th_cfg_add_section(&cfg, "server", tmpcfg); 1080 th_cfg_add_section(&cfg, "server", tmpcfg);
1073 1081
1074 tmpcfg = NULL; 1082 tmpcfg = NULL;
1075 th_cfg_add_comment(&tmpcfg, "Enable logging"); 1083 th_cfg_add_comment(&tmpcfg, "Enable logging");
1076 th_cfg_add_string(&tmpcfg, "host", &optServer, optServer); 1084 th_cfg_add_bool(&tmpcfg, "log", &optLogEnable, optLogEnable);
1077 th_cfg_add_comment(&tmpcfg, "Default port to connect to (8002 = public room, 8003 = passion pit, 8005 = members only)"); 1085 th_cfg_add_comment(&tmpcfg, "Log filename format");
1078 th_cfg_add_int(&tmpcfg, "port", &optPort, optPort); 1086 th_cfg_add_string(&tmpcfg, "logformat", &optLogFilename, optLogFilename);
1079 th_cfg_add_section(&cfg, "server", tmpcfg);
1080 1087
1081 #ifdef __WIN32 1088 #ifdef __WIN32
1082 { 1089 {
1083 char tmpPath[MAX_PATH]; 1090 char tmpPath[MAX_PATH];
1084 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) 1091 if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK)
1132 THERR("Could not allocate userhash. Fatal error.\n"); 1139 THERR("Could not allocate userhash. Fatal error.\n");
1133 return -105; 1140 return -105;
1134 } 1141 }
1135 1142
1136 /* Open logfile */ 1143 /* Open logfile */
1137 if (optLogFilename) { 1144 logFileOpen();
1138 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
1139 1145
1140 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) {
1141 THERR("Could not open logfile for appending!\n");
1142 return -9;
1143 }
1144 }
1145 1146
1146 if (!nn_network_init()) { 1147 if (!nn_network_init()) {
1147 THERR("Could not initialize network subsystem.\n"); 1148 THERR("Could not initialize network subsystem.\n");
1148 goto err_exit; 1149 goto err_exit;
1149 } else 1150 } else
1575 if (networkInit) 1576 if (networkInit)
1576 nn_network_close(); 1577 nn_network_close();
1577 1578
1578 THMSG(1, "Connection terminated.\n"); 1579 THMSG(1, "Connection terminated.\n");
1579 1580
1580 if (optLogFile) { 1581 logFileClose();
1581 THMSG(1, "Closing logfile.\n");
1582 fclose(optLogFile);
1583 }
1584 1582
1585 return 0; 1583 return 0;
1586 } 1584 }