PILog ready to use

This commit is contained in:
2024-09-19 17:26:58 +03:00
parent aa963a4bda
commit 4acab04895
4 changed files with 53 additions and 14 deletions

View File

@@ -92,8 +92,10 @@ PILog::~PILog() {
void PILog::setDir(const PIString & d) {
stopAndWait();
log_dir = d;
PIDir::make(log_dir);
newFile();
if (output[File]) {
PIDir::make(log_dir);
newFile();
}
start();
}
@@ -164,7 +166,6 @@ PICout PILog::makePICout(PIObject * context, Level cat) {
void PILog::enqueue(const PIString & msg, Level cat) {
if (log_file.isClosed()) return;
auto t = PIDateTime::fromSystemTime(PISystemTime::current());
PIMutexLocker ml(log_mutex);
queue.enqueue({cat, t, msg});
@@ -190,9 +191,11 @@ void PILog::newFile() {
void PILog::run() {
if (split_tm.elapsed() >= split_time) {
split_tm.reset();
newFile();
if (output[File]) {
if (split_tm.elapsed() >= split_time) {
split_tm.reset();
newFile();
}
}
log_mutex.lock();
if (queue.isEmpty()) {
@@ -210,10 +213,17 @@ void PILog::run() {
auto qi = queue.dequeue();
log_mutex.unlock();
auto str = entryToString(qi);
log_ts << str << "\n";
if (qi.cat == Level::Error)
piCerr << str;
else
piCout << str;
if (log_file.isOpened()) log_ts << str << "\n";
if (output[Console]) {
PICout out(qi.cat == Level::Error ? piCerr : piCout);
if (color_console) {
switch (qi.cat) {
case Level::Error: out << PICoutManipulators::Red; break;
case Level::Warning: out << PICoutManipulators::Yellow; break;
default: break;
}
}
out << str;
}
}
}

View File

@@ -48,12 +48,27 @@ public:
Debug,
};
enum Output {
File = 0x1,
Console = 0x2,
All = 0xFF,
};
//! \~english Set output target \"o\" to \"on\".
void setOutput(Output o, bool on = true) { output.setFlag(o, on); }
//! \~english Returns prefix for filename.
PIString logName() const { return log_name; }
//! \~english Set prefix for filename. Should be set \b before \a setDir()!
void setLogName(const PIString & n) { log_name = n; }
//! \~english Returns if color for console output enabled.
bool colorConsole() const { return color_console; }
//! \~english Set color for console output enabled. True by default.
void setColorConsole(bool yes) { color_console = yes; }
//! \~english Returns directory for log files.
PIString dir() const { return log_dir; }
@@ -89,10 +104,16 @@ public:
//! \~english Set maximum level. All levels greater than \"l\" will be ignored. Default if \a Level::Debug.
void setLevel(Level l);
//! \~english Returns PICout for Level::Error level.
PICout error(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Warning level.
PICout warning(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Info level.
PICout info(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Debug level.
PICout debug(PIObject * context = nullptr);
//! \~english Write all queued lines and stop. Also called in destructor.
@@ -122,7 +143,9 @@ private:
PIString log_dir, timestamp_format, line_format, line_format_p, log_name;
PIQueue<Entry> queue;
PIMap<Level, int> id_by_cat;
Level max_level = Level::Debug;
Level max_level = Level::Debug;
PIFlags<Output> output = All;
bool color_console = true;
int part_number = -1, cout_id = -1;
};

View File

@@ -220,7 +220,6 @@ void PIKbdListener::readKeyboard() {
#ifdef WINDOWS
INPUT_RECORD ir;
ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret));
// piCout << ir.EventType;
switch (ir.EventType) {
case KEY_EVENT: {
KEY_EVENT_RECORD ker = ir.Event.KeyEvent;
@@ -542,6 +541,11 @@ void PIKbdListener::readKeyboard() {
}
void PIKbdListener::stop() {
PIThread::stop();
}
void PIKbdListener::end() {
// cout << "list end" << endl;
#ifdef WINDOWS

View File

@@ -180,6 +180,8 @@ public:
void readKeyboard();
void stop();
//! Returns if keyboard listening is active (not running!)
bool isActive() { return is_active; }