Skip to content

Commit

Permalink
Move logs into folder with TLEs/settings; date and rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
JVital2013 committed Oct 27, 2023
1 parent e4748c3 commit 2efab2d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ assignees: ''

**Version (Eg, 1.0.0, CI Build 171)**

**Logs after the crash (satdump.log)**
**Logs after the crash** (satdump.log from ~/.config/satdump or %appdata%\satdump)

**Other info (Eg, Screenshots) / Files useful for debugging (CADU, etc)**
2 changes: 1 addition & 1 deletion src-core/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace satdump

#ifdef _WIN32
if (std::filesystem::exists("satdump_cfg.json"))
user_path = ".";
user_path = "./config";
else
user_path = std::string(getenv("APPDATA")) + "/satdump";
#elif __ANDROID__
Expand Down
53 changes: 33 additions & 20 deletions src-core/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <iostream>
#include <ctime>
#include <algorithm>
#include <filesystem>
#ifdef __ANDROID__
#include <android/log.h>
static char ag_LogTag[] = "SatDump";
Expand All @@ -11,10 +12,6 @@ static char ag_LogTag[] = "SatDump";
#include <windows.h>
#include <wincon.h>
#endif
#ifdef __APPLE__
#include <sysdir.h>
#include <glob.h>
#endif

#include "init.h"

Expand Down Expand Up @@ -151,9 +148,19 @@ namespace slog

FileSink::FileSink(std::string path)
{
outf = std::ofstream(path);
}
if (!std::filesystem::exists(std::filesystem::path(path).parent_path()))
std::filesystem::create_directories(std::filesystem::path(path).parent_path());

int suffix = 0;
std::string log_path = path + ".log";
while(std::filesystem::exists(log_path))
{
suffix++;
log_path = path + "-" + std::to_string(suffix) + ".log";
}

outf = std::ofstream(log_path);
}
FileSink::~FileSink()
{
outf.close();
Expand Down Expand Up @@ -278,23 +285,29 @@ void initLogger()

void initFileSink()
{
//Get log path
try
{
#ifdef __APPLE__
char library_glob[PATH_MAX];
glob_t globbuf;
sysdir_search_path_enumeration_state search_state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_LIBRARY, SYSDIR_DOMAIN_MASK_USER);
sysdir_get_next_search_path_enumeration(search_state, library_glob);
glob(library_glob, GLOB_TILDE, nullptr, &globbuf);
std::string log_path = std::string(globbuf.gl_pathv[0]) + "/Logs/satdump.log";
globfree(&globbuf);
#elif defined(_WIN32)
std::string log_path = satdump::user_path + "/satdump.log";
#else
std::string log_path = "satdump.log";
#endif
//Clear old logs
char timebuffer[16];
struct tm *timeinfo;
time_t current_time = time(NULL);
timeinfo = localtime(&current_time);
strftime(timebuffer, sizeof(timebuffer), "%Y%m%d", timeinfo);
int delete_before = atoi(timebuffer) - 2;
for (const auto& user_file : std::filesystem::directory_iterator(satdump::user_path + "/"))
{
std::string file_name = user_file.path().filename().string();
if (file_name.size() < 26)
continue;
int log_date = atoi(file_name.substr(8, 8).c_str());
if (log_date != 0 && log_date < delete_before)
std::filesystem::remove(user_file);
}

file_sink = std::make_shared<slog::FileSink>(log_path);
//Create new log
strftime(timebuffer, sizeof(timebuffer), "%Y%m%dT%H%M%S", timeinfo);
file_sink = std::make_shared<slog::FileSink>(satdump::user_path + "/satdump-" + std::string(timebuffer));
logger->add_sink(file_sink);
// file_sink->set_pattern("[%D - %T] (%L) %v");
file_sink->set_level(slog::LOG_TRACE);
Expand Down

0 comments on commit 2efab2d

Please sign in to comment.