| 148 | #endif |
| 149 | |
| 150 | [[nodiscard]] std::unique_ptr<deinit_t> init(int min_log_level, const std::string &log_file) { |
| 151 | if (sink) { |
| 152 | // Deinitialize the logging system before reinitializing it. This can probably only ever be hit in tests. |
| 153 | deinit(); |
| 154 | } |
| 155 | |
| 156 | // Check if the log file exists and handle backup |
| 157 | std::string backup_log_file = log_file + ".backup"; |
| 158 | if (std::filesystem::exists(log_file)) { |
| 159 | try { |
| 160 | // If the backup file exists, remove it |
| 161 | if (std::filesystem::exists(backup_log_file)) { |
| 162 | std::filesystem::remove(backup_log_file); |
| 163 | } |
| 164 | // Rename the current log file to the backup name |
| 165 | std::filesystem::rename(log_file, backup_log_file); |
| 166 | } catch (std::exception& e) { |
| 167 | std::cout << "Failed to rotate log file: " << e.what() << std::endl; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #ifndef __ANDROID__ |
| 172 | setup_av_logging(min_log_level); |
| 173 | setup_libdisplaydevice_logging(min_log_level); |
| 174 | #endif |
| 175 | |
| 176 | sink = boost::make_shared<text_sink>(); |
| 177 | |
| 178 | #ifndef SUNSHINE_TESTS |
| 179 | boost::shared_ptr<std::ostream> stream {&std::cout, boost::null_deleter()}; |
| 180 | sink->locked_backend()->add_stream(stream); |
| 181 | #endif |
| 182 | |
| 183 | sink->locked_backend()->add_stream(boost::make_shared<std::ofstream>(log_file)); |
| 184 | sink->set_filter(severity >= min_log_level); |
| 185 | sink->set_formatter(&formatter); |
| 186 | |
| 187 | // Flush after each log record to ensure log file contents on disk isn't stale. |
| 188 | // This is particularly important when running from a Windows service. |
| 189 | sink->locked_backend()->auto_flush(true); |
| 190 | |
| 191 | bl::core::get()->add_sink(sink); |
| 192 | |
| 193 | #ifdef __ANDROID__ |
| 194 | auto android_sink = boost::make_shared<sinks::synchronous_sink<android_sink_backend>>(); |
| 195 | bl::core::get()->add_sink(android_sink); |
| 196 | #endif |
| 197 | return std::make_unique<deinit_t>(); |
| 198 | } |
| 199 | |
| 200 | #ifndef __ANDROID__ |
| 201 | void setup_av_logging(int min_log_level) { |
nothing calls this directly
no test coverage detected