Skip to content
/ cx Public

cx - a set of simple C++23 utilities. The goal is not a fully featured implementation, just usable enough for simple projects.

Notifications You must be signed in to change notification settings

sakchhu/cx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CX - C++ Utilities

CX is a set of C++ header-only utilities for simple applications in C++23. Right now, it provides a customizable stream-based logging implementation.


cx/logger.hpp

Features

  • Stream based Logger : Write to any std::ostream& object
  • Multiple Log Levels : Trace, Debug, Info, Warn, Error
  • Configurable Verbosity & Prefix : Three verbosity levels for logger messages, and ability to add a prefix
  • Formatted Log Messages : Supports std::format/std::print style logging, so you don't have to use any of those to build the message string.

Basic Usage

Logger initialization

With file stream:

auto file = std::ofstream{"file.txt"};
auto logger = cx::Logger{file};

With a string stream:

auto buffer = std::ostringstream{};
auto logger = cx::Logger{buffer};

Example

#include <cx/logger.hpp>
#include <iostream> // for std::cout

int main() {
    auto logger = cx::Logger{std::cout}; // log to std::cout
    logger.minimumLevel = cx::LogLevel::Trace;
    logger.verbosity = cx::LogMsgVerbosity::Maximum;
    logger.prefix = "Example";

    std::string user = "alice";
    std::string ip = "192.168.1.55";
    std::vector<int> values{1, 2, 3, 4};

    logger.info("User {} connected from {}", user, ip);
    logger.debug("Vector value contains {} elements", values.size());
    logger.trace("value[0] = {}", values[0]);
    logger.trace("value =  {}", values);
}
  • Sample Output
[Example] | [INFO] | [2025-09-11 05:16:17.827337558] | getting_started.cpp(13:17) | [int main()] | User alice connected from 192.168.1.55
[Example] | [DEBUG] | [2025-09-11 05:16:17.827402093] | getting_started.cpp(14:18) | [int main()] | Vector value contains 4 elements
[Example] | [TRACE] | [2025-09-11 05:16:17.827416903] | getting_started.cpp(15:18) | [int main()] | value[0] = 1
[Example] | [TRACE] | [2025-09-11 05:16:17.827428743] | getting_started.cpp(16:18) | [int main()] | value =  [1, 2, 3, 4]

Verbosity Options and Outputs

Minimum

[DEBUG] | [2025-10-11 07:07:19.706234057] | Message

Standard

[DEBUG] | [2025-10-11 07:07:19.706234057] | main.cpp(12:30) | Message

Maximum

[DEBUG] | [2025-10-11 07:07:19.706234057] | main.cpp(12:30) | [int main()] | Message

About

cx - a set of simple C++23 utilities. The goal is not a fully featured implementation, just usable enough for simple projects.

Resources

Stars

Watchers

Forks