Skip to content

Commit

Permalink
updated documentation and added %SC / %EC flags for stripping color c…
Browse files Browse the repository at this point in the history
…odes
  • Loading branch information
NewYaroslav committed Oct 6, 2024
1 parent 6b3a5de commit 6f07afc
Show file tree
Hide file tree
Showing 21 changed files with 3,801 additions and 3,385 deletions.
344 changes: 344 additions & 0 deletions README-RU.md

Large diffs are not rendered by default.

43 changes: 27 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# LogIt++ Library
![LogIt++ Logo](docs/logo-640x320.png)

[Читать на русском](README-RU.md)

## Introduction

**LogIt++** is a flexible and versatile C++ logging library that supports various backends and stream-based output. It provides an easy-to-use interface for logging messages with different severity levels and allows customization of log formats and destinations.
Expand Down Expand Up @@ -32,6 +34,7 @@ int main() {

float a = 123.456f;
int b = 789;
int c = 899;
const char* someStr = "Hello, World!";

// Basic logging using macros
Expand All @@ -41,24 +44,24 @@ int main() {

// Formatted logging
LOG_PRINTF_INFO("Formatted log: value of a = %.2f", a);
LOG_FORMAT_WARN("Warning! Values: a = %.2f, b = %d", a, b);
LOG_FORMAT_WARN("%.4d", b, c);

// Error and fatal logs
LOG_ERROR("An error occurred with value b =", b);
LOG_ERROR("An error occurred", b);
LOG_FATAL("Fatal error. Terminating application.");

// Conditional logging
LOG_ERROR_IF(b < 0, "Value of b is negative");
LOG_WARN_IF(a > 100, "Value of a exceeds 100");

// Stream-based logging with short and long names
LOG_S_INFO << "Logging a float: " << a << ", and an int: " << b;
LOG_S_ERROR << "Error occurred in the system";
LOG_S_INFO() << "Logging a float: " << a << ", and an int: " << b;
LOG_S_ERROR() << "Error occurred in the system";
LOGIT_STREAM_WARN() << "Warning: potential issue detected with value: " << someStr;

// Using LOGIT_TRACE for tracing function execution
LOGIT_TRACE0(); // Trace without arguments
LOG_TRACE("Entering main function with variable a =", a);
LOG_PRINT_TRACE("Entering main function with variable a =", a);

// Wait for all asynchronous logs to be processed
LOGIT_WAIT();
Expand All @@ -67,6 +70,8 @@ int main() {
}
```

For more usage examples, please refer to the `examples` folder in the repository, where you can find detailed demonstrations of various logging scenarios and configurations.

## Log Message Formatting Flags
LogIt++ supports customizable log message formatting using format flags. You can define how each log message should appear by including placeholders for different pieces of information such as the timestamp, log level, file name, function name, and message.

Expand Down Expand Up @@ -113,14 +118,16 @@ Below is a list of supported format flags:

- `%^`: Start color formatting
- `%$`: End color formatting

- `%SC`: Start removing color codes (Strip Color)
- `%EC`: End removing color codes (End Color)

- *Message Flags*:

- `%v`: The log message content

## Shortened Logging Macros

LogIt++ provides shortened versions of logging macros when LOGIT_SHORT_NAME is defined. These macros allow for concise logging across different log levels, including both standard and stream-based logging.
LogIt++ provides shortened versions of logging macros when `LOGIT_SHORT_NAME` is defined. These macros allow for concise logging across different log levels, including both standard and stream-based logging.

### Available TRACE-level macros:

Expand Down Expand Up @@ -158,15 +165,16 @@ LOG_TRACE_PRINTF("Formatted trace: value = %d", value);
```
## Configuration Macros
LogIt++ provides several macros that allow for customization and configuration. Below are the available configuration macros:
- **LOGIT_BASE_PATH**: Defines the base path used for log file paths. If `LOGIT_BASE_PATH` is not defined or is empty ({}), the full path from `__FILE__` will be used for log file paths. You can override this to specify a custom base path for your log files.
- **LOGIT_BASE_PATH**: Defines the base path used for log file paths. If `LOGIT_BASE_PATH` is not defined or is empty (`{}`), the full path from `__FILE__` will be used for log file paths. You can override this to specify a custom base path for your log files.
```cpp
#define LOGIT_BASE_PATH "/path/to/your/project"
```

- **LOGIT_DEFAULT_COLOR**: Defines the default color for console output. If `LOGIT_DEFAULT_COLOR` is not defined, it defaults to TextColor::LightGray. You can set a custom console text color by overriding this macro.
- **LOGIT_DEFAULT_COLOR**: Defines the default color for console output. If `LOGIT_DEFAULT_COLOR` is not defined, it defaults to `TextColor::LightGray`. You can set a custom console text color by overriding this macro.

```cpp
#define LOGIT_DEFAULT_COLOR TextColor::Green
Expand Down Expand Up @@ -196,25 +204,25 @@ LogIt++ provides several macros that allow for customization and configuration.
#define LOGIT_FILE_LOGGER_AUTO_DELETE_DAYS 60 // Keep logs for 60 days
```
- **LOGIT_FILE_LOGGER_PATTERN**: Defines the default log pattern for file-based loggers. This pattern controls the formatting of log messages written to log files, including timestamp, filename, line number, function, and thread information. If `LOGIT_FILE_LOGGER_PATTERN` is not defined, it defaults to `[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %v`.
- **LOGIT_FILE_LOGGER_PATTERN**: Defines the default log pattern for file-based loggers. This pattern controls the formatting of log messages written to log files, including timestamp, filename, line number, function, and thread information. If `LOGIT_FILE_LOGGER_PATTERN` is not defined, it defaults to `[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %SC%v`.
```cpp
#define LOGIT_FILE_LOGGER_PATTERN "[%Y-%m-%d %H:%M:%S.%e] [%l] %v"
#define LOGIT_FILE_LOGGER_PATTERN "[%Y-%m-%d %H:%M:%S.%e] [%l] %SC%v"
```

- **LOGIT_UNIQUE_FILE_LOGGER_PATH**: Defines the default directory path for unique log files. If LOGIT_UNIQUE_FILE_LOGGER_PATH is not defined, it defaults to "data/logs/unique_logs". You can specify a custom path for unique log files.
- **LOGIT_UNIQUE_FILE_LOGGER_PATH**: Defines the default directory path for unique log files. If `LOGIT_UNIQUE_FILE_LOGGER_PATH` is not defined, it defaults to *"data/logs/unique_logs"*. You can specify a custom path for unique log files.

```cpp
#define LOGIT_UNIQUE_FILE_LOGGER_PATH "/custom/unique/log/directory"
```
- **LOGIT_UNIQUE_FILE_LOGGER_PATTERN**: Defines the default log pattern for unique file-based loggers. If LOGIT_UNIQUE_FILE_LOGGER_PATTERN is not defined, it defaults to "%v". You can customize this pattern to control the format of log messages in unique files.
- **LOGIT_UNIQUE_FILE_LOGGER_PATTERN**: Defines the default log pattern for unique file-based loggers. If `LOGIT_UNIQUE_FILE_LOGGER_PATTERN` is not defined, it defaults to `"%v"`. You can customize this pattern to control the format of log messages in unique files.
```cpp
#define LOGIT_UNIQUE_FILE_LOGGER_PATTERN "[%Y-%m-%d %H:%M:%S.%e] [%l] %v"
#define LOGIT_UNIQUE_FILE_LOGGER_PATTERN "%v"
```

- **LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH**: Defines the length of the hash used in unique log file names. If LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH is not defined, it defaults to 8 characters. This ensures that unique filenames are generated for each log entry.
- **LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH**: Defines the length of the hash used in unique log file names. If `LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH` is not defined, it defaults to `8` characters. This ensures that unique filenames are generated for each log entry.

```cpp
#define LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH 12 // Set hash length to 12 characters
Expand All @@ -225,6 +233,7 @@ LogIt++ provides several macros that allow for customization and configuration.
- **LOGIT_USE_FMT_LIB**: Enables the use of the fmt library for string formatting.
## Example Format
To define a custom format for your log messages, you can use the following method:
```cpp
Expand All @@ -241,6 +250,7 @@ logit::Logger::get_instance().add_logger(
```

## Custom Logger Backend and Formatter

You can extend LogIt++ by implementing your own loggers and formatters. Here’s how:

### Custom Logger Example
Expand Down Expand Up @@ -302,6 +312,7 @@ public:
```

## Installation

LogIt++ is a header-only library. To integrate it into your project, follow these steps:

1. Clone the repository with its submodules:
Expand All @@ -322,7 +333,7 @@ If you are using an IDE like **Visual Studio** or **CLion**, you can add the inc

4. (Optional) Enable fmt support:

LogIt++ supports the **fmt** library for advanced string formatting, which is also included as a submodule. To enable `fmt` in LogIt++, define the macro `LOGIT_USE_FMT_LIB` in your project:
LogIt++ supports the *fmt* library for advanced string formatting, which is also included as a submodule. To enable *fmt* in LogIt++, define the macro `LOGIT_USE_FMT_LIB` in your project:

```cpp
#define LOGIT_USE_FMT_LIB
Expand Down
8 changes: 5 additions & 3 deletions docs/mainpage.dox
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

\section intro_sec Introduction

**LogIt++** is a flexible and versatile C++ logging library that supports various backends and stream-based output. It provides an easy-to-use interface for logging messages with different severity levels and allows customization of log formats and destinations.

**LogIt++** is a flexible and versatile C++ logging library that supports various backends and stream-based output. It provides an easy-to-use interface for logging messages with different severity levels and allows customization of log formats and destinations.
The library combines the simplicity of macro-based logging similar to **IceCream-Cpp** and the configurability of logging backends and formats like **spdlog**.
LogIt++ is fully compatible with C++11, ensuring support for a wide range of compilers and systems.

\section features_sec Features

Expand Down Expand Up @@ -111,7 +111,9 @@ Below is a list of all supported format flags and their meanings:
\subsection color_flags Color Formatting
- `%^`: Start color formatting
- `%$`: End color formatting

- `%SC`: Start removing color codes (Strip Color)
- `%EC`: End removing color codes (End Color)

\subsection message_flags Message Content
- `%v`: The log message content

Expand Down
40 changes: 20 additions & 20 deletions include/log-it/LogItConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,61 @@
/// \brief Defines the base path used for log file paths.
/// If `LOGIT_BASE_PATH` is not defined or is empty (`{}`), the full path from `__FILE__` will be used for log file paths.
#ifndef LOGIT_BASE_PATH
#define LOGIT_BASE_PATH {}
#define LOGIT_BASE_PATH {}
#endif

/// \brief Defines the default color for console output.
/// If LOGIT_DEFAULT_COLOR is not defined, defaults to `TextColor::LightGray`.
///
/// This macro allows setting a default console text color for log messages.
#ifndef LOGIT_DEFAULT_COLOR
#define LOGIT_DEFAULT_COLOR TextColor::LightGray
#define LOGIT_DEFAULT_COLOR TextColor::LightGray
#endif

#ifndef LOGIT_COLOR_TRACE
#define LOGIT_COLOR_TRACE TextColor::DarkGray
#define LOGIT_COLOR_TRACE TextColor::DarkGray
#endif

#ifndef LOGIT_COLOR_DEBUG
#define LOGIT_COLOR_DEBUG TextColor::Blue
#define LOGIT_COLOR_DEBUG TextColor::Blue
#endif

#ifndef LOGIT_COLOR_INFO
#define LOGIT_COLOR_INFO TextColor::Green
#define LOGIT_COLOR_INFO TextColor::Green
#endif

#ifndef LOGIT_COLOR_WARN
#define LOGIT_COLOR_WARN TextColor::Yellow
#define LOGIT_COLOR_WARN TextColor::Yellow
#endif

#ifndef LOGIT_COLOR_ERROR
#define LOGIT_COLOR_ERROR TextColor::Red
#define LOGIT_COLOR_ERROR TextColor::Red
#endif

#ifndef LOGIT_COLOR_FATAL
#define LOGIT_COLOR_FATAL TextColor::Magenta
#define LOGIT_COLOR_FATAL TextColor::Magenta
#endif

#ifndef LOGIT_COLOR_DEFAULT
#define LOGIT_COLOR_DEFAULT TextColor::White
#define LOGIT_COLOR_DEFAULT TextColor::White
#endif

/// \brief Macro to get the current timestamp in milliseconds.
/// If LOGIT_CURRENT_TIMESTAMP_MS is not defined, it uses `std::chrono` to return the current time in milliseconds.
///
/// This macro can be overridden to provide a custom method for generating timestamps if needed.
#ifndef LOGIT_CURRENT_TIMESTAMP_MS
#define LOGIT_CURRENT_TIMESTAMP_MS() \
(std::chrono::duration_cast<std::chrono::milliseconds>( \
std::chrono::system_clock::now().time_since_epoch()).count())
#define LOGIT_CURRENT_TIMESTAMP_MS() \
(std::chrono::duration_cast<std::chrono::milliseconds>( \
std::chrono::system_clock::now().time_since_epoch()).count())
#endif

/// \brief Defines the default log pattern for the console logger.
/// If `LOGIT_CONSOLE_PATTERN` is not defined, it defaults to "%H:%M:%S.%e | %^%v%$".
///
/// This pattern controls the formatting of log messages sent to the console, including timestamp, message, and color.
#ifndef LOGIT_CONSOLE_PATTERN
#define LOGIT_CONSOLE_PATTERN "%H:%M:%S.%e | %^%v%$"
#define LOGIT_CONSOLE_PATTERN "%H:%M:%S.%e | %^%v%$"
#endif

/// \brief Defines the default directory path for log files.
Expand All @@ -70,7 +70,7 @@
/// This macro specifies the directory where regular log files will be stored.
/// The default path is relative to the application's execution directory.
#ifndef LOGIT_FILE_LOGGER_PATH
#define LOGIT_FILE_LOGGER_PATH "data/logs"
#define LOGIT_FILE_LOGGER_PATH "data/logs"
#endif

/// \brief Defines the default directory path for unique log files.
Expand All @@ -79,36 +79,36 @@
/// This macro specifies the directory where unique log files, created by `UniqueFileLogger`,
/// will be stored. Each log message will generate a new file in this directory.
#ifndef LOGIT_UNIQUE_FILE_LOGGER_PATH
#define LOGIT_UNIQUE_FILE_LOGGER_PATH "data/logs/unique_logs"
#define LOGIT_UNIQUE_FILE_LOGGER_PATH "data/logs/unique_logs"
#endif

/// \brief Defines the number of days after which old log files are deleted.
/// If `LOGIT_FILE_LOGGER_AUTO_DELETE_DAYS` is not defined, it defaults to 30 days.
///
/// This macro controls the log file retention policy by specifying the maximum age of log files.
#ifndef LOGIT_FILE_LOGGER_AUTO_DELETE_DAYS
#define LOGIT_FILE_LOGGER_AUTO_DELETE_DAYS 30
#define LOGIT_FILE_LOGGER_AUTO_DELETE_DAYS 30
#endif

/// \brief Defines the default log pattern for file-based loggers.
/// If `LOGIT_FILE_LOGGER_PATTERN` is not defined, it defaults to
/// "[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %v".
/// "[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %SC%v".
///
/// This pattern controls the formatting of log messages written to log files, including timestamp, filename, line number, function, and thread information.
#ifndef LOGIT_FILE_LOGGER_PATTERN
#define LOGIT_FILE_LOGGER_PATTERN "[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %v"
#define LOGIT_FILE_LOGGER_PATTERN "[%Y-%m-%d %H:%M:%S.%e] [%ffn:%#] [%!] [thread:%t] [%l] %SC%v"
#endif

/// \brief Defines the default log pattern for unique file-based loggers.
/// If `LOGIT_UNIQUE_FILE_LOGGER_PATTERN` is not defined, it defaults to "%v".
#ifndef LOGIT_UNIQUE_FILE_LOGGER_PATTERN
#define LOGIT_UNIQUE_FILE_LOGGER_PATTERN "%v"
#define LOGIT_UNIQUE_FILE_LOGGER_PATTERN "%v"
#endif

/// \brief Defines the default hash length for unique file names.
/// If `LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH` is not defined, it defaults to 8.
#ifndef LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH
#define LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH 8
#define LOGIT_UNIQUE_FILE_LOGGER_HASH_LENGTH 8
#endif

#endif // _LOGIT_CONFIG_HPP_INCLUDED
Loading

0 comments on commit 6f07afc

Please sign in to comment.