Ed Tanous | 50c50c2 | 2017-05-12 16:58:06 -0700 | [diff] [blame] | 1 | /** ========================================================================== |
| 2 | * 2013 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes |
| 3 | * with no warranties. This code is yours to share, use and modify with no |
| 4 | * strings attached and no restrictions or obligations. |
| 5 | * |
| 6 | * For more information see g3log/LICENSE or refer refer to http://unlicense.org |
| 7 | * ============================================================================*/ |
| 8 | |
| 9 | #include "g3log/filesink.hpp" |
| 10 | #include "filesinkhelper.ipp" |
| 11 | #include <cassert> |
| 12 | |
| 13 | namespace g3 { |
| 14 | using namespace internal; |
| 15 | |
| 16 | |
| 17 | FileSink::FileSink(const std::string &log_prefix, const std::string &log_directory, const std::string& logger_id) |
| 18 | : _log_file_with_path(log_directory) |
| 19 | , _log_prefix_backup(log_prefix) |
| 20 | , _outptr(new std::ofstream) |
| 21 | { |
| 22 | _log_prefix_backup = prefixSanityFix(log_prefix); |
| 23 | if (!isValidFilename(_log_prefix_backup)) { |
| 24 | std::cerr << "g3log: forced abort due to illegal log prefix [" << log_prefix << "]" << std::endl; |
| 25 | abort(); |
| 26 | } |
| 27 | |
| 28 | std::string file_name = createLogFileName(_log_prefix_backup, logger_id); |
| 29 | _log_file_with_path = pathSanityFix(_log_file_with_path, file_name); |
| 30 | _outptr = createLogFile(_log_file_with_path); |
| 31 | |
| 32 | if (!_outptr) { |
| 33 | std::cerr << "Cannot write log file to location, attempting current directory" << std::endl; |
| 34 | _log_file_with_path = "./" + file_name; |
| 35 | _outptr = createLogFile(_log_file_with_path); |
| 36 | } |
| 37 | assert(_outptr && "cannot open log file at startup"); |
| 38 | addLogFileHeader(); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | FileSink::~FileSink() { |
| 43 | std::string exit_msg {"g3log g3FileSink shutdown at: "}; |
| 44 | exit_msg.append(localtime_formatted(systemtime_now(), internal::time_formatted)).append({"\n"}); |
| 45 | filestream() << exit_msg << std::flush; |
| 46 | |
| 47 | exit_msg.append({"Log file at: ["}).append(_log_file_with_path).append({"]\n"}); |
| 48 | std::cerr << exit_msg << std::flush; |
| 49 | } |
| 50 | |
| 51 | // The actual log receiving function |
| 52 | void FileSink::fileWrite(LogMessageMover message) { |
| 53 | std::ofstream &out(filestream()); |
| 54 | out << message.get().toString() << std::flush; |
| 55 | } |
| 56 | |
| 57 | std::string FileSink::changeLogFile(const std::string &directory, const std::string &logger_id) { |
| 58 | |
| 59 | auto now = g3::systemtime_now(); |
| 60 | auto now_formatted = g3::localtime_formatted(now, {internal::date_formatted + " " + internal::time_formatted}); |
| 61 | |
| 62 | std::string file_name = createLogFileName(_log_prefix_backup, logger_id); |
| 63 | std::string prospect_log = directory + file_name; |
| 64 | std::unique_ptr<std::ofstream> log_stream = createLogFile(prospect_log); |
| 65 | if (nullptr == log_stream) { |
| 66 | filestream() << "\n" << now_formatted << " Unable to change log file. Illegal filename or busy? Unsuccessful log name was: " << prospect_log; |
| 67 | return {}; // no success |
| 68 | } |
| 69 | |
| 70 | addLogFileHeader(); |
| 71 | std::ostringstream ss_change; |
| 72 | ss_change << "\n\tChanging log file from : " << _log_file_with_path; |
| 73 | ss_change << "\n\tto new location: " << prospect_log << "\n"; |
| 74 | filestream() << now_formatted << ss_change.str(); |
| 75 | ss_change.str(""); |
| 76 | |
| 77 | std::string old_log = _log_file_with_path; |
| 78 | _log_file_with_path = prospect_log; |
| 79 | _outptr = std::move(log_stream); |
| 80 | ss_change << "\n\tNew log file. The previous log file was at: "; |
| 81 | ss_change << old_log; |
| 82 | filestream() << now_formatted << ss_change.str(); |
| 83 | return _log_file_with_path; |
| 84 | } |
| 85 | std::string FileSink::fileName() { |
| 86 | return _log_file_with_path; |
| 87 | } |
| 88 | void FileSink::addLogFileHeader() { |
| 89 | filestream() << header(); |
| 90 | } |
| 91 | |
| 92 | } // g3 |