PICout improvement:

* renamed private members for more clear code
 * registerExternalBufferID() method to obtain unique ID for withExternalBuffer()
 * PICoutManipulators::PICoutStdStream enum for select stream (stdout or stderr)
 * Constructors now accept optional stream
 * piCerr and piCerrObj macros

PIDir::temporary() moved to "mkdtemp"

PILog:
 * now 4 levels
 * you can set max level
 * Error writes to piCerr
This commit is contained in:
2024-09-16 16:06:07 +03:00
parent 9d4357c066
commit 000ce2a54d
11 changed files with 357 additions and 320 deletions

View File

@@ -41,11 +41,18 @@ public:
PILog();
~PILog();
enum class Level {
Error,
Warning,
Info,
Debug,
};
//! \~english Returns prefix for filename.
PIString applicationName() const { return app_name; }
PIString logName() const { return log_name; }
//! \~english Set prefix for filename. Should be set \b before \a setDir()!
void setApplicationName(const PIString & n) { app_name = n; }
void setLogName(const PIString & n) { log_name = n; }
//! \~english Returns directory for log files.
@@ -75,27 +82,30 @@ public:
//! \~english Set line format. "t" is timestamp, "c" is category and "m" is message. Default is "t - c: m".
void setLineFormat(const PIString & f);
PICout debug(PIObject * context = nullptr);
PICout warning(PIObject * context = nullptr);
//! \~english Returns maximum level.
Level level() const { return max_level; }
//! \~english Set maximum level. All levels greater than \"l\" will be ignored. Default if \a Level::Debug.
void setLevel(Level l);
PICout error(PIObject * context = nullptr);
PICout warning(PIObject * context = nullptr);
PICout info(PIObject * context = nullptr);
PICout debug(PIObject * context = nullptr);
//! \~english Write all queued lines and stop. Also called in destructor.
void stop();
private:
enum class Category {
Debug,
Warning,
Error
};
EVENT_HANDLER2(void, coutDone, int, id, PIString *, buff);
PICout makePICout(PIObject * context, Category cat);
void enqueue(const PIString & msg, Category cat = Category::Debug);
PICout makePICout(PIObject * context, Level cat);
void enqueue(const PIString & msg, Level cat = Level::Debug);
struct Entry {
Category cat;
Level cat;
PIDateTime time;
PIString msg;
};
@@ -104,14 +114,15 @@ private:
void newFile();
void run() override;
PIMutex log_mutex, cout_mutex;
PIMutex log_mutex;
PIFile log_file;
PIIOTextStream log_ts;
PITimeMeasurer split_tm;
PISystemTime split_time;
PIString log_dir, timestamp_format, line_format, line_format_p, app_name;
PIString log_dir, timestamp_format, line_format, line_format_p, log_name;
PIQueue<Entry> queue;
PIMap<int, Category> cout_cat_by_id;
PIMap<Level, int> id_by_cat;
Level max_level = Level::Debug;
int part_number = -1, cout_id = -1;
};