bool initlogging(const char *identity, int facility_, int consolethres, int filethres, int syslogthres, bool logtimestamp) { facility = facility_ & 7; timestamp = logtimestamp; if(consolethres >= 0) consolethreshold = min(consolethres, (int)ACLOG_NUM); if(filethres >= 0) filethreshold = min(filethres, (int)ACLOG_NUM); if(syslogthres >= 0) syslogthreshold = min(syslogthres, (int)ACLOG_NUM); if(ident != identity) copystring(ident, identity); formatstring(ident_full)("ACR[%s]", identity); if(syslogthreshold < ACLOG_NUM) { #ifdef AC_USE_SYSLOG openlog(ident_full, LOG_NDELAY, facilities[facility]); #else if((logsock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM)) == ENET_SOCKET_NULL || enet_address_set_host(&logdest, "localhost") < 0) syslogthreshold = ACLOG_NUM; #endif } static int lognum = 0; formatstring(filepath)("serverlog_%s_%s.part%d.txt", timestring(true), identity, ++lognum); if(fp) { fclose(fp); fp = NULL; } if(filethreshold < ACLOG_NUM) { fp = fopen(filepath, "w"); if(!fp) printf("failed to open \"%s\" for writing\n", filepath); } defformatstring(msg)("logging started: console(%s), file(%s", levelname[consolethreshold], levelname[fp ? filethreshold : ACLOG_NUM]); if(fp) concatformatstring(msg, ", \"%s\"", filepath); concatformatstring(msg, "), syslog(%s", levelname[syslogthreshold]); if(syslogthreshold < ACLOG_NUM) concatformatstring(msg, ", \"%s\", local%d", ident_full, facility); concatformatstring(msg, "), timestamp(%s)", timestamp ? "ENABLED" : "DISABLED"); enabled = consolethreshold < ACLOG_NUM || fp || syslogthreshold < ACLOG_NUM; if(enabled) printf("%s\n", msg); return enabled; }
bool listdir(const char *dir, const char *ext, vector<char *> &files) { int extsize = ext ? (int)strlen(ext)+1 : 0; #if defined(WIN32) defformatstring(pathname)("%s\\*.%s", dir, ext ? ext : "*"); WIN32_FIND_DATA FindFileData; HANDLE Find = FindFirstFile(path(pathname), &FindFileData); if(Find != INVALID_HANDLE_VALUE) { do { if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) files.add(newstring(FindFileData.cFileName, (int)strlen(FindFileData.cFileName) - extsize)); } while(FindNextFile(Find, &FindFileData)); FindClose(Find); return true; } #else string pathname; copystring(pathname, dir); DIR *d = opendir(path(pathname)); if(d) { struct dirent *de, b; while(!readdir_r(d, &b, &de) && de != NULL) { bool isreg = false; #ifdef _DIRENT_HAVE_D_TYPE if(de->d_type == DT_REG) isreg = true; else if(de->d_type == DT_UNKNOWN) #endif { struct stat s; int dl = (int)strlen(pathname); concatformatstring(pathname, "/%s", de->d_name); isreg = !lstat(pathname, &s) && S_ISREG(s.st_mode); pathname[dl] = '\0'; } if(isreg) { if(!ext) files.add(newstring(de->d_name)); else { int namelength = (int)strlen(de->d_name) - extsize; if(namelength > 0 && de->d_name[namelength] == '.' && strncmp(de->d_name+namelength+1, ext, extsize-1)==0) files.add(newstring(de->d_name, namelength)); } } } closedir(d); return true; } #endif else return false; }
bool listsubdir(const char *dir, vector<char *> &subdirs) { #if defined(WIN32) defformatstring(pathname)("%s\\*", dir); WIN32_FIND_DATA FindFileData; HANDLE Find = FindFirstFile(path(pathname), &FindFileData); if(Find != INVALID_HANDLE_VALUE) { do { if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && FindFileData.cFileName[0] != '.') subdirs.add(newstring(FindFileData.cFileName)); } while(FindNextFile(Find, &FindFileData)); FindClose(Find); return true; } #else string pathname; copystring(pathname, dir); DIR *d = opendir(path(pathname)); if(d) { struct dirent *de, b; while(!readdir_r(d, &b, &de) && de != NULL) { #ifdef _DIRENT_HAVE_D_TYPE if(de->d_type == DT_DIR && de->d_name[0] != '.') subdirs.add(newstring(de->d_name)); else if(de->d_type == DT_UNKNOWN && de->d_name[0] != '.') #endif { struct stat s; int dl = (int)strlen(pathname); concatformatstring(pathname, "/%s", de->d_name); if(!lstat(pathname, &s) && S_ISDIR(s.st_mode) && de->d_name[0] != '.') subdirs.add(newstring(de->d_name)); pathname[dl] = '\0'; } } closedir(d); return true; } #endif else return false; }