| 47 | } |
| 48 | |
| 49 | void LogManager::configure(json config, const filesystem::path& defaultDir) |
| 50 | { |
| 51 | std::lock_guard<std::mutex> grd(entry_mutex); |
| 52 | |
| 53 | /*-------------------------------------------------*\ |
| 54 | | If the log is not open, create a new log file | |
| 55 | \*-------------------------------------------------*/ |
| 56 | if(!log_stream.is_open()) |
| 57 | { |
| 58 | std::string logname = "OpenRGB_#.log"; |
| 59 | |
| 60 | /*-------------------------------------------------*\ |
| 61 | | If the logfile is defined in the configuration, | |
| 62 | | use the configured name | |
| 63 | \*-------------------------------------------------*/ |
| 64 | if(config.contains("logfile")) |
| 65 | { |
| 66 | const json& logfile_obj = config["logfile"]; |
| 67 | if(logfile_obj.is_string()) |
| 68 | { |
| 69 | std::string tmpname = config["logfile"]; |
| 70 | if(!tmpname.empty()) |
| 71 | { |
| 72 | logname = tmpname; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /*-------------------------------------------------*\ |
| 78 | | If the # symbol is found in the log file name, | |
| 79 | | replace it with a timestamp | |
| 80 | \*-------------------------------------------------*/ |
| 81 | time_t t = time(0); |
| 82 | struct tm* tmp = localtime(&t); |
| 83 | char time_string[64]; |
| 84 | snprintf(time_string, 64, "%04d%02d%02d_%02d%02d%02d", 1900 + tmp->tm_year, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); |
| 85 | |
| 86 | size_t oct = logname.find("#"); |
| 87 | if(oct != logname.npos) |
| 88 | { |
| 89 | logname.replace(oct, 1, time_string); |
| 90 | } |
| 91 | |
| 92 | /*-------------------------------------------------*\ |
| 93 | | If the path is relative, use logs dir | |
| 94 | \*-------------------------------------------------*/ |
| 95 | filesystem::path p = filesystem::u8path(logname); |
| 96 | if(p.is_relative()) |
| 97 | { |
| 98 | p = defaultDir / "logs" / logname; |
| 99 | } |
| 100 | filesystem::create_directories(p.parent_path()); |
| 101 | |
| 102 | /*-------------------------------------------------*\ |
| 103 | | Open the logfile | |
| 104 | \*-------------------------------------------------*/ |
| 105 | log_stream.open(p); |
| 106 | |