Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielblagy/yelloger
Simple thread-safe single-header C++ 17 logger.
https://github.com/danielblagy/yelloger
cpp-library cpp17 header-only log-priorities logger thread-safe thread-safe-logger timestamps-format
Last synced: 2 months ago
JSON representation
Simple thread-safe single-header C++ 17 logger.
- Host: GitHub
- URL: https://github.com/danielblagy/yelloger
- Owner: danielblagy
- License: mit
- Created: 2021-02-08T14:21:54.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-02T05:50:36.000Z (over 3 years ago)
- Last Synced: 2023-07-30T17:33:10.890Z (over 1 year ago)
- Topics: cpp-library, cpp17, header-only, log-priorities, logger, thread-safe, thread-safe-logger, timestamps-format
- Language: C++
- Homepage:
- Size: 21.5 KB
- Stars: 15
- Watchers: 1
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Yelloger
* [General Info](#motivation)
* [Reference](#reference-contents)
## A simple thread-safe single-header C++ 17 logger.
### Motivation
This logger was initially made for a [Youtube tutorial](https://youtube.com/playlist?list=PL5Lk2LPoiyAKcw7T-_FB_4BNrWkxfwnus).
### Include
This is a header-only library consisting of one header file. Simply copy the [include/yelloger.h](include/yelloger.h) file and `#include` it in your project.
### Simple Example
Console logging (the simplest use case)
```cpp
#includeint main()
{
const char* name = "User";
Yellog::Info("Hello %s", name);
return 0;
}
```
Output:
> 15:07:31 15-02-2021 [Info] Hello User### Quick Start
Yellog doesn't need to be instantiated, just include the header and use it like this
```cpp
Yellog::Info("Infotmation %d", int_value);
```
, also there is no need to put newline character at the end of the message, it will be done automatically.## Reference Contents
* [Log Priorities](#log-priorities)
* [Logging](#logging)
* [File Output](#file-output)
* [Timestamps](#timestamps)## Reference
### Log Priorities
The default log priority is `Yellog::InfoPriority`. You can set priority by calling
```cpp
Yellog::SetPriority(Yellog::DebugPriority); // e.g. Yellog::DebugPriority
```Possible values:
```cpp
Yellog::TracePriority
Yellog::DebugPriority
Yellog::InfoPriority
Yellog::WarnPriority
Yellog::ErrorPriority
Yellog::CriticalPriority
```
You can get priority by calling
```cpp
Yellog::GetPriority(); // will return Yellog::InfoPriority if Yellog::SetPriority hasn't been called before
```### Logging
To log:
```cpp
Yellog::Trace(const char* message, Args... args) // log a message with trace priority
Yellog::Debug(const char* message, Args... args) // log a message with debug priority
Yellog::Info(const char* message, Args... args) // log a message with info priority
Yellog::Warn(const char* message, Args... args) // log a message with warn priority
Yellog::Error(const char* message, Args... args) // log a message with error priority
Yellog::Critical(const char* message, Args... args) // log a message with critical priority
```As args you can provide primitives and C-strings. Formatting follows [printf format](https://www.cplusplus.com/reference/cstdio/printf/).
### File Output
To enable file output, call
```cpp
Yellog::EnableFileOutput("mylogpath/mylog.txt");
```
before using the logger.
Optionally, you can provide no path
```cpp
Yellog::EnableFileOutput();
```
then, the logs will be saved in '/log.txt'.
To get the current filepath for file logging, call
```cpp
Yellog::GetFilepath();
```
if file output was not enabled, the filepath will contain NULL, otherwise a const char* value will be returned.
To check if file output was enabled and file was successfully opened, call
```cpp
Yellog::IsFileOutputEnabled(); // returns true if success, false if failure
```### Timestamps
Format follows ctime [strftime format specification](https://www.cplusplus.com/reference/ctime/strftime/).
Default format is "%T %d-%m-%Y" (e.g. 13:20:25 14-02-2021).
4 spaces are added automatically to the end of timestamp each time the message is logged.
To set a log timestamp format, call
```cpp
Yellog::SetTimestampFormat("%c"); // e.g. Thu Aug 23 14:55:02 2001
```
To get the current log timestamp format, call
```cpp
Yellog::GetTimestampFormat(); // e.g. "13:20:25 14-02-2021"
```