| 37 | static char loglevelenabled[ACLOG_NUM]; |
| 38 | |
| 39 | bool initlogging(const char *identity, int facility_, int consolethres, int filethres, int syslogthres, bool logtimestamp, const char *logfilepath) |
| 40 | { |
| 41 | static void *logworkerthread_id = NULL; |
| 42 | extern int logworkerthread(void *nop); |
| 43 | if(!logworkerthread_id) logworkerthread_id = sl_createthread(logworkerthread, NULL); // always start, if dedicated server |
| 44 | facility = facility_; |
| 45 | timestamp = logtimestamp; |
| 46 | if(consolethres >= 0) consolethreshold = min(consolethres, (int)ACLOG_NUM); |
| 47 | if(filethres >= 0) filethreshold = min(filethres, (int)ACLOG_NUM); |
| 48 | if(syslogthres >= 0) syslogthreshold = min(syslogthres, (int)ACLOG_NUM); |
| 49 | facility &= 7; |
| 50 | formatstring(ident)("AssaultCube[%s]", identity); |
| 51 | if(syslogthreshold < ACLOG_NUM) |
| 52 | { |
| 53 | #ifdef AC_USE_SYSLOG |
| 54 | openlog(ident, LOG_NDELAY, facilities[facility]); |
| 55 | #else |
| 56 | if((logsock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM)) == ENET_SOCKET_NULL || enet_address_set_host(&logdest, "localhost") < 0) syslogthreshold = ACLOG_NUM; |
| 57 | #endif |
| 58 | } |
| 59 | formatstring(filepath)("%sserverlog_%s_%s.txt", logfilepath, timestring(true), identity); |
| 60 | path(filepath); |
| 61 | if(fp) { fclose(fp); fp = NULL; } |
| 62 | if(filethreshold < ACLOG_NUM) |
| 63 | { |
| 64 | fp = fopen(filepath, "w"); |
| 65 | if(!fp) printf("failed to open \"%s\" for writing\n", filepath); |
| 66 | } |
| 67 | defformatstring(msg)("logging started: console(%s), file(%s", levelname[consolethreshold], levelname[fp ? filethreshold : ACLOG_NUM]); |
| 68 | if(fp) concatformatstring(msg, ", \"%s\"", filepath); |
| 69 | concatformatstring(msg, "), syslog(%s", levelname[syslogthreshold]); |
| 70 | if(syslogthreshold < ACLOG_NUM) concatformatstring(msg, ", \"%s\", local%d", ident, facility); |
| 71 | concatformatstring(msg, "), timestamp(%s)", timestamp ? "ENABLED" : "DISABLED"); |
| 72 | enabled = consolethreshold < ACLOG_NUM || fp || syslogthreshold < ACLOG_NUM; |
| 73 | loopi(ACLOG_NUM) loglevelenabled[i] = (consolethreshold <= i ? LOGTARGET_CONSOLE : 0) | (fp && filethreshold <= i ? LOGTARGET_FILE : 0) | (syslogthreshold <= i ? LOGTARGET_SYSLOG : 0); |
| 74 | if(enabled) printf("%s\n", msg); |
| 75 | return enabled; |
| 76 | } |
| 77 | |
| 78 | void exitlogging() |
| 79 | { |
no test coverage detected