void simpleLog_init(const char* _logFileName, bool _useTimeStamps, int _logLevel, bool append) { if (_logFileName != NULL) { // NOTE: this causes a memory leack, as it is never freed. // but it is used till the end of the applications runtime anyway // -> no problem logFileName = util_allocStrCpy(_logFileName); // delete the logFile, and try writing to it FILE* file = NULL; if (logFileName != NULL) { if (append) { file = FOPEN(logFileName, "a"); } else { file = FOPEN(logFileName, "w"); } } if (file != NULL) { // make the file empty FPRINTF(file, "%s", ""); fclose(file); file = NULL; } else { // report the error to stderr FPRINTF(stderr, "Failed writing to the log file \"%s\".\n%s", logFileName, "We will continue logging to stdout."); } // make sure the dir of the log file exists char* logFileDir = util_allocStrCpy(logFileName); if (!util_getParentDir(logFileDir)) { simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "Failed to evaluate the parent dir of the config file: %s", logFileName); } else if (!util_makeDir(logFileDir, true)) { simpleLog_logL(SIMPLELOG_LEVEL_ERROR, "Failed to create the parent dir of the config file: %s", logFileDir); } free(logFileDir); } else { simpleLog_logL(-1, "No log file name supplied -> logging to stdout and stderr", useTimeStamps ? "yes" : "no", logLevel); logFileName = NULL; } useTimeStamps = _useTimeStamps; logLevel = _logLevel; simpleLog_logL(-1, "[logging started (time-stamps: %s / logLevel: %i)]", useTimeStamps ? "yes" : "no", logLevel); }
bool util_makeDirRecursive(const char* dirPath) { if (!util_fileExists(dirPath)) { char parentDir[strlen(dirPath)+1]; bool hasParent = util_getParentDir(dirPath, parentDir); if (hasParent) { bool parentExists = util_makeDirRecursive(parentDir); if (parentExists) { return util_makeDir(dirPath); } } return false; } return true; }
bool util_findDir(const char* dirs[], unsigned int numDirs, const char* relativeDirPath, char* absoluteDirPath, bool searchOnlyWriteable, bool create) { bool found = false; // check if it is an absolute file path if (util_fileExists(relativeDirPath)) { STRCPY(absoluteDirPath, relativeDirPath); found = true; } if (searchOnlyWriteable && numDirs > 1) { numDirs = 1; } unsigned int d; for (d=0; d < numDirs && !found; ++d) { // do the following: tmpPath = dirs[d] + sPS + relativeFilePath char* tmpPath = util_allocStr(strlen(dirs[d]) + 1 + strlen(relativeDirPath)); //char tmpPath[strlen(dirs[d]) + 1 + strlen(relativeDirPath) + 1]; tmpPath[0]= '\0'; tmpPath = STRCAT(tmpPath, dirs[d]); tmpPath = STRCAT(tmpPath, sPS); tmpPath = STRCAT(tmpPath, relativeDirPath); if (util_fileExists(tmpPath)) { STRCPY(absoluteDirPath, tmpPath); found = true; } free(tmpPath); } // not found -> create it if (!found && create && numDirs >= 1) { STRCAT(absoluteDirPath, dirs[0]); STRCAT(absoluteDirPath, sPS); STRCAT(absoluteDirPath, relativeDirPath); found = util_makeDir(absoluteDirPath); } return found; }