File: Logging.cpp

package info (click to toggle)
ausaxs 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,592 kB
  • sloc: cpp: 49,853; ansic: 6,901; python: 730; makefile: 18
file content (58 lines) | stat: -rw-r--r-- 1,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#include <utility/Logging.h>

#include <io/File.h>
#include <constants/Version.h>
#include <settings/GeneralSettings.h>

#include <iostream>
#include <fstream>
#include <chrono>
#include <ctime>

using namespace ausaxs;

struct {
    std::ofstream log_file;
    void start(const std::string& name) {
        std::cout << "Logging to " << settings::general::cache + name + "_log.txt" << std::endl;
        auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
        std::string current_time = std::ctime(&t);
        log_file = std::ofstream(settings::general::cache + name + "_log.txt");
        log_file << "AUSAXS " << constants::version << " log file\nstarted at " << current_time << "\n";
    }

    void log(std::string_view msg) {
        if (!log_file.is_open()) {return;}
        log_file << msg << "\n";
    }

    void log_console(std::string_view msg) {
        if (!log_file.is_open()) {return;}
        log_file << "CONSOLE: " << msg << std::endl;
    }

    void log_critical(std::string_view msg) {
        log(msg);
        log_file.flush();
    }
} logobj;

void logging::start(std::string_view name) {
    logobj.start(std::string(name));
}

void logging::log_critical(std::string_view msg) {
    logobj.log_console(msg);
    logobj.log("CRITICAL: " + std::string(msg));
}

void logging::log(std::string_view msg) {
    logobj.log(msg);
}

void logging::log_console(std::string_view msg) {
    logobj.log_console(msg);
}