forked from aristocratos/btop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
129 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "btop_log.hpp" | ||
|
||
#include <filesystem> | ||
#include <iostream> | ||
#include <mutex> | ||
#include <string> | ||
|
||
#include "btop_shared.hpp" | ||
#include "btop_tools.hpp" | ||
|
||
namespace fs = std::filesystem; | ||
|
||
namespace Logger { | ||
using namespace Tools; | ||
std::atomic<bool> busy(false); | ||
bool first = true; | ||
const string tdf = "%Y/%m/%d (%T) | "; | ||
|
||
size_t loglevel; | ||
fs::path logfile; | ||
|
||
//* Wrapper for lowering priviliges if using SUID bit and currently isn't using real userid | ||
class lose_priv { | ||
int status = -1; | ||
|
||
public: | ||
lose_priv() { | ||
if (geteuid() != Global::real_uid) { | ||
this->status = seteuid(Global::real_uid); | ||
} | ||
} | ||
~lose_priv() { | ||
if (status == 0) { | ||
status = seteuid(Global::set_uid); | ||
} | ||
} | ||
}; | ||
|
||
void set(const string& level) { loglevel = v_index(log_levels, level); } | ||
|
||
void log_write(const size_t level, const string& msg) { | ||
if (loglevel < level or logfile.empty()) { | ||
return; | ||
} | ||
atomic_lock lck(busy, true); | ||
lose_priv neutered{}; | ||
std::error_code ec; | ||
try { | ||
if (fs::exists(logfile) and fs::file_size(logfile, ec) > 1024 << 10 and not ec) { | ||
auto old_log = logfile; | ||
old_log += ".1"; | ||
|
||
if (fs::exists(old_log)) { | ||
fs::remove(old_log, ec); | ||
} | ||
|
||
if (not ec) { | ||
fs::rename(logfile, old_log, ec); | ||
} | ||
} | ||
if (not ec) { | ||
std::ofstream lwrite(logfile, std::ios::app); | ||
if (first) { | ||
first = false; | ||
lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n"; | ||
} | ||
lwrite << strf_time(tdf) << log_levels.at(level) << ": " << msg << "\n"; | ||
} | ||
else { | ||
logfile.clear(); | ||
} | ||
} | ||
catch (const std::exception& e) { | ||
logfile.clear(); | ||
throw std::runtime_error("Exception in Logger::log_write() : " + string{e.what()}); | ||
} | ||
} | ||
} // namespace Logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <filesystem> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace Logger { | ||
const std::vector<std::string> log_levels = { | ||
"DISABLED", | ||
"ERROR", | ||
"WARNING", | ||
"INFO", | ||
"DEBUG", | ||
}; | ||
extern std::filesystem::path logfile; | ||
|
||
//* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG" | ||
void set(const std::string& level); | ||
|
||
void log_write(const size_t level, const std::string& msg); | ||
inline void error(const std::string msg) { log_write(1, msg); } | ||
inline void warning(const std::string msg) { log_write(2, msg); } | ||
inline void info(const std::string msg) { log_write(3, msg); } | ||
inline void debug(const std::string msg) { log_write(4, msg); } | ||
} // namespace Logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters