Coder Social home page Coder Social logo

danielblagy / yelloger Goto Github PK

View Code? Open in Web Editor NEW
17.0 1.0 6.0 22 KB

Simple thread-safe single-header C++ 17 logger.

License: MIT License

C++ 100.00%
cpp-library logger thread-safe-logger thread-safe timestamps-format log-priorities cpp17 header-only

yelloger's Introduction

Yelloger

A simple thread-safe single-header C++ 17 logger.

Motivation

This logger was initially made for a Youtube tutorial.

Include

This is a header-only library consisting of one header file. Simply copy the include/yelloger.h file and #include it in your project.

Simple Example

Console logging (the simplest use case)

#include <yelloger.h>

int 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

	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

Reference

Log Priorities

The default log priority is Yellog::InfoPriority. You can set priority by calling

	Yellog::SetPriority(Yellog::DebugPriority);	// e.g. Yellog::DebugPriority

Possible values:

	Yellog::TracePriority
	Yellog::DebugPriority
	Yellog::InfoPriority
	Yellog::WarnPriority
	Yellog::ErrorPriority
	Yellog::CriticalPriority

You can get priority by calling

	Yellog::GetPriority();	// will return Yellog::InfoPriority if Yellog::SetPriority hasn't been called before

Logging

To log:

	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.

File Output

To enable file output, call

	Yellog::EnableFileOutput("mylogpath/mylog.txt");

before using the logger.

Optionally, you can provide no path

	Yellog::EnableFileOutput();

then, the logs will be saved in '/log.txt'.

To get the current filepath for file logging, call

	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

	Yellog::IsFileOutputEnabled();	// returns true if success, false if failure

Timestamps

Format follows ctime strftime format specification.
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

	Yellog::SetTimestampFormat("%c");	// e.g. Thu Aug 23 14:55:02 2001

To get the current log timestamp format, call

	Yellog::GetTimestampFormat();	// e.g. "13:20:25  14-02-2021"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.