common_util
Loading...
Searching...
No Matches
1. Quick Start Guide

1.1. Overview

common_util is a general-purpose C++ utility library, implemented in modern C++ syntax (C++11 and above), using cutl as the namespace. The naming convention of all interfaces is consistent with the STL, and it can be used as an extensions of C++ standard library. You can use common_util just like using the STL.

1.2. Directory Structure

./common_util
├── build # Cache directory for program building
├── docs # Documentation directory
│   ├── quick_start # Quick Start Guide
│   └── reference # API interface reference
├── publish # Release directory - stores the final results of the build when using the release configuration
│   ├── common_util # Published common_util library
│   │   ├── include # published common_util header files
│   │   └── lib # published common_util static library
│   └── common_util_demo # Published common_util_demo executable program
├── script # Script directory
└── src # Source code directory
├── common_util # common_util source code directory
└── usage_demo # common_util_demo source code directory

1.3. Usage

The simple steps are as follows:

‍1. Include the common_util.h header file.

  1. Initialize the library and register a callback function for logging.
  2. Use the library.

A detail example as follows:

++
#pragma once
#include <iostream>
#include <map>
#include <mutex>
#include <thread>
#include "common_util.h"
std::string loglevel2flag(cutl::loglevel level)
{
static std::map<cutl::loglevel, std::string> levelMap = {
{cutl::loglevel::debug_level, "[D]"},
{cutl::loglevel::info_level, "[I]"},
{cutl::loglevel::warn_level, "[W]"},
{cutl::loglevel::error_level, "[E]"},
};
auto itr = levelMap.find(level);
if (itr != levelMap.end())
{
return itr->second;
}
return "[?]";
}
static std::mutex g_log_mtx_;
void static library_log_func(cutl::loglevel level, const std::string &msg)
{
std::lock_guard<std::mutex> lock(g_log_mtx_);
auto curTime = cutl::fmt_timestamp_ms(cutl::timestamp(cutl::timeunit::ms));
auto threadId = std::this_thread::get_id();
if (cutl::loglevel::error_level == level)
{
std::cerr << "[" << curTime << "]" << loglevel2flag(level) << "]" << threadId << "](cutl) " << msg << std::endl;
}
else if (cutl::loglevel::debug_level == level)
{
// print debug log
}
else
{
std::cout << "[" << curTime << "]" << loglevel2flag(level) << "]" << threadId << "](cutl) " << msg << std::endl;
}
}
int main(int argc, char *argv[])
{
// 库的初始化
cutl::library_init(library_log_func);
// 数据格式化
std::cout << "fmt_uint: " << cutl::fmt_uint(12, 5) << std::endl;
std::cout << "fmt_double: " << cutl::fmt_double(3.141592653, 4) << std::endl;
std::cout << "fmt_filesize: " << cutl::fmt_filesize(33600) << std::endl;
// 获取系统当前时间
auto now = cutl::datetime::now();
std::cout << "current time(UTC time): " << now.utctime() << std::endl;
std::cout << "current time(local time): " << now.format() << std::endl;
return 0;
}
static datetime now()
Get a datetime object for the current system time.
The index of the header files of common_util library. For convenient to use, you can only include com...
void library_init(LogFuncType log_func)
Initialize the library.
loglevel
The type of log level.
Definition logtype.h:31
std::string fmt_timestamp_ms(uint64_t ms, bool local=true)
Format a timestamp to a human-readable string.
std::string fmt_uint(uint64_t val, uint8_t width=0, char fill='0')
Format uint64 value to a string with a given width and fill character.
std::string fmt_filesize(uint64_t size, bool simplify=true, int precision=1)
Format a file size to a human-readable string with a given precision.
std::string fmt_double(double val, int precision=2)
Format double value to a string with a given precision.

Running result:

[2024-05-18 16:01:03.477][I]]0x7ff844a9b100](cutl) [config.cpp:39:library_init] common_util library initialized, version:1.0.0
fmt_uint: 00012
fmt_double: 3.1416
fmt_filesize: 32.8K
current time(UTC time): 2024-05-18 08:01:03.478
current time(local time): 2024-05-18 16:01:03.478

1.4. Demo

Directory src/usage_demo contains usage examples for each module.

config.hpp # Initialization configuration
datetime.hpp # Usage of the datetime class
filepath.hpp # Usage of the filepath class
fileutil.hpp # File system related operations
strfmt.hpp # String formatting
strutil.hpp # String related operations
sysutil.hpp # System call related operations
timecount.hpp # Function timer class
timeutil.hpp # Time related operations
verutil.hpp # Version number related operations