Skip to content

Commit

Permalink
Refactoring CMake, md files
Browse files Browse the repository at this point in the history
  • Loading branch information
tuduongquyet authored and deemar committed Apr 11, 2024
1 parent 67063c7 commit 7fe61ca
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 203 deletions.
62 changes: 25 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# create by lsm <lsm@skybility.com>
# cmake
# LICENSE: Apache License 2.0
# Copyright (c) Hardy Simpson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/

cmake_minimum_required(VERSION 2.8.5)

Expand All @@ -9,20 +22,15 @@ message(STATUS "path : ${CMAKE_FIND_ROOT_PATH}")

set(CMAKE_MODULE_PATH ${zlog_SOURCE_DIR}/cmake)

#=====================================
# version of zlog
#=====================================
SET(CPACK_PACKAGE_VERSION_MAJOR "1")
SET(CPACK_PACKAGE_VERSION_MINOR "2")
SET(CPACK_PACKAGE_VERSION_PATCH "12")
SET(CPACK_RPM_PACKAGE_RELEASE 1) #release version.
SET(CPACK_RPM_PACKAGE_RELEASE 1) # release version.
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET(zlog_ver ${CPACK_PACKAGE_VERSION})
SET(zlog_so_ver ${CPACK_PACKAGE_VERSION_MAJOR})
SET(ZLOG_VERSION ${CPACK_PACKAGE_VERSION})
SET(ZLOG_SO_VERSION ${CPACK_PACKAGE_VERSION_MAJOR})

#=======================================================

message(STATUS "plateform : ${CMAKE_SYSTEM}")
message(STATUS "platform : ${CMAKE_SYSTEM}")

add_definitions("-g -Wall -Wstrict-prototypes")
set(CMAKE_C_FLAGS "-std=c99 -pedantic -D_DEFAULT_SOURCE")
Expand All @@ -31,48 +39,28 @@ set(CMAKE_C_FLAGS_RELEASE "-O2")

if (WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWINVER=0x0500 -D_WIN32_WINNT=0x0500 ")
endif()

#=====================================================
# include dir
# include_directories(include)
#=====================================================
endif ()

#=====================================================
# lib output path.
cmake_policy(SET CMP0015 NEW)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/lib")
# bin output path.

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/bin")
#=====================================================

#=====================================================
# link path.
link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
#=====================================================

#=====================================================
# library depend.
set(Need_THREAD 1)

if (WIN32)
set(Need_UNIXEM 1)
endif()
endif ()

include(cmake/LoadLibraries.cmake)
#=====================================================

#========================================================
# sub dir
add_subdirectory(src)
add_subdirectory(cpack)
#========================================================

#========================================================
# for unittest, call "cmake .. -DUNIT_TEST=on"
if(UNIT_TEST)
if (UNIT_TEST)
enable_testing()
add_subdirectory(test)
endif()
#========================================================

endif ()
25 changes: 18 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

zlog is a reliable, high-performance, thread safe, flexible, clear-model, pure C logging library.

Actually, in the C world there was NO good logging library for applications like logback in java or log4cxx in c++. Using printf can work, but can not be redirected or reformatted easily. syslog is slow and is designed for system use.
Actually, in the C world there was NO good logging library for applications like logback in java or log4cxx in c++.
Using printf can work, but can not be redirected or reformatted easily. syslog is slow and is designed for system use.
So I wrote zlog.
It is faster, safer and more powerful than log4c. So it can be widely used.

Expand All @@ -24,25 +25,32 @@ make PREFIX=/usr/local/
sudo make PREFIX=/usr/local/ install
```

PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure your program can find zlog library.
PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure
your program can find zlog library.

```bash
$ sudo vi /etc/ld.so.conf
/usr/local/lib
$ sudo ldconfig
```

Before running a real program, make sure libzlog.so is in the directory where the system's dynamic lib loader can find it. The command metioned above are for linux. Other systems will need a similar set of actions.
Before running a real program, make sure libzlog.so is in the directory where the system's dynamic lib loader can find
it. The command metioned above are for linux. Other systems will need a similar set of actions.

## 2. Configuration file

There are 3 important concepts in zlog: categories, formats and rules.

Categories specify different kinds of log entries. In the zlog source code, category is a `zlog_cateogory_t *` variable. In your program, different categories for the log entries will distinguish them from each other.
Categories specify different kinds of log entries. In the zlog source code, category is a `zlog_cateogory_t *` variable.
In your program, different categories for the log entries will distinguish them from each other.

Formats describe log patterns, such as: with or without time stamp, source file, source line.

Rules consist of category, level, output file (or other channel) and format. In brief, if the category string in a rule in the configuration file equals the name of a category variable in the source, then they match. Still there is complex match range of category. Rule decouples variable conditions. For example, log4j must specify a level for each logger(or inherit from father logger). That's not convenient when each grade of logger has its own level for output(child logger output at the level of debug, when father logger output at the level of error)
Rules consist of category, level, output file (or other channel) and format. In brief, if the category string in a rule
in the configuration file equals the name of a category variable in the source, then they match. Still there is complex
match range of category. Rule decouples variable conditions. For example, log4j must specify a level for each logger(or
inherit from father logger). That's not convenient when each grade of logger has its own level for output(child logger
output at the level of debug, when father logger output at the level of error)

Now create a configuration file. The function zlog_init takes the files path as its only argument.

Expand All @@ -55,7 +63,9 @@ simple = "%m%n"
my_cat.DEBUG >stdout; simple
```

In the configuration file log messages in the category `my_cat` and a level of DEBUG or higher are output to standard output, with the format of simple `%m - usermessage %n - newline`. If you want to direct out to a file and limit the files maximum size, use this configuration
In the configuration file log messages in the category `my_cat` and a level of DEBUG or higher are output to standard
output, with the format of simple `%m - usermessage %n - newline`. If you want to direct out to a file and limit the
files maximum size, use this configuration

```config
my_cat.DEBUG "/var/log/aa.log", 1M; simple
Expand Down Expand Up @@ -111,7 +121,8 @@ hello, zlog
* syslog model, better than log4j model
* log format customization
* multiple output destinations including static file path, dynamic file path, stdout, stderr, syslog, user-defined output
* multiple output destinations including static file path, dynamic file path, stdout, stderr, syslog, user-defined
output
* runtime manually or automatically refresh configure(safely)
* high-performance, 250'000 logs/second on my laptop, about 1000 times faster than syslog(3) with rsyslogd
* user-defined log level
Expand Down
19 changes: 9 additions & 10 deletions cmake/FindUnixem.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
if (NOT UNIXEM_FOUND)
if (NOT UNIXEM_INCLUDE_DIR)
find_path(UNIXEM_INCLUDE_DIR
NAMES
NAMES
unixem/unixem.h
ONLY_CMAKE_FIND_ROOT_PATH
ONLY_CMAKE_FIND_ROOT_PATH
)
endif()
endif ()

if (NOT UNIXEM_LIBRARY)
find_library(UNIXEM_LIBRARY
NAMES unixem
ONLY_CMAKE_FIND_ROOT_PATH
NAMES unixem
ONLY_CMAKE_FIND_ROOT_PATH
)
endif (NOT UNIXEM_LIBRARY)

message(STATUS " UNIXEM_INCLUDE_DIR = ${UNIXEM_INCLUDE_DIR}")
message(STATUS " UNIXEM_LIBRARY = ${UNIXEM_LIBRARY}")

if (UNIXEM_INCLUDE_DIR AND UNIXEM_LIBRARY)
set(UNIXEM_FOUND TRUE)
endif (UNIXEM_INCLUDE_DIR AND UNIXEM_LIBRARY)
endif()

endif ()
21 changes: 12 additions & 9 deletions cmake/LoadLibraries.cmake
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#=======================================================
# =======================================================
# 支持多线程
# 对于需要多线程的库,使用以下命令包含连接库:
# target_link_libraries(xxx ${CMAKE_THREAD_PREFER_PTHREAD})
#=======================================================
if(Need_THREAD)
# =======================================================
if (Need_THREAD)
find_package(Threads REQUIRED)
if(NOT CMAKE_THREAD_PREFER_PTHREAD)

if (NOT CMAKE_THREAD_PREFER_PTHREAD)
set(CMAKE_THREAD_PREFER_PTHREAD ${CMAKE_THREAD_LIBS_INIT})
endif()
endif ()

message(STATUS "thread lib : ${CMAKE_THREAD_PREFER_PTHREAD}")
endif(Need_THREAD)
endif (Need_THREAD)

if(Need_UNIXEM)
if (Need_UNIXEM)
find_package(Unixem)

if (NOT UNIXEM_FOUND)
message(FATAL_ERROR "unixem lib not found!")
endif()
endif()
endif ()
endif ()
9 changes: 4 additions & 5 deletions cmake/toolchain-mingw64.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# this one is important
SET(CMAKE_SYSTEM_NAME Windows)

Expand All @@ -7,7 +6,7 @@ find_path(MINGW_BIN_PATH gcc.exe PATHS c:/mingw64 d:/mingw64 c:/mingw-build/ming

if (MINGW_PATH_NOTFOUND)
message(FATAL "mingw64 not found!")
endif()
endif ()

get_filename_component(MINGW_PATH ${MINGW_BIN_PATH} PATH)

Expand All @@ -19,10 +18,10 @@ SET(CMAKE_CXX_COMPILER g++)
SET(CMAKE_RC_COMPILER windres)
set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")

#SET(_CMAKE_TOOLCHAIN_PREFIX x86_64-w64-mingw32-)
# SET(_CMAKE_TOOLCHAIN_PREFIX x86_64-w64-mingw32-)
SET(_CMAKE_TOOLCHAIN_LOCATION ${MINGW_BIN_PATH})

# where is the target environment
# where is the target environment
SET(CMAKE_FIND_ROOT_PATH ${MINGW_PATH} ${MINGW_PATH}/x86_64-w64-mingw32)

SET(CMAKE_SYSTEM_INCLUDE_PATH "${CMAKE_SYSTEM_INCLUDE_PATH} ${MINGW_PATH}/include ${MINGW_PATH}/x86_64-w64-mingw32/include")
Expand All @@ -34,7 +33,7 @@ add_definitions("-D_POSIX")

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

36 changes: 11 additions & 25 deletions cpack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
# create by lsm <lsm@skybility.com>

#======================================
# vendor info
#======================================
SET(CPACK_PACKAGE_VENDOR "zlog")
SET(CPACK_PACKAGE_CONTACT "HardySimpson1984@gmail.com")
SET(CPACK_RPM_PACKAGE_LICENSE "Apache License Version 2.0")

#======================================
# default install prefix.
#======================================
SET(CPACK_PACKAGING_INSTALL_PREFIX "/usr/local")
SET(CPACK_RPM_PACKAGE_GROUP "System Environment/Base")

#=================================
# set platform.
#=================================
message(STATUS "system process is ${CMAKE_HOST_SYSTEM_PROCESSOR}")
message(STATUS "system process is ${CMAKE_SYSTEM_PROCESSOR}")

IF (NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()
endif ()

IF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64|amd64")
IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64|amd64")
set(CMAKE_SYSTEM_PROCESSOR x86_64)
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE amd64)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "x86_64")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc64|ppc64")
ELSEIF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc64|ppc64")
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE powerpc64)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "ppc64")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc|ppc")
ELSEIF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc|ppc")
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE powerpc)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "ppc")
ELSE()
ELSE ()
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE i386)
ENDIF()
SET(CPACK_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
ENDIF ()

SET(CPACK_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
SET(CPACK_TOPLEVEL_TAG "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")

#======================================
# generator setting.
#======================================
SET(CPACK_CMAKE_GENERATOR "Unix Makefiles")

if (WIN32)
SET(CPACK_GENERATOR "NSIS")
else()
else ()
SET(CPACK_GENERATOR "RPM;DEB")
endif()
endif ()

#======================================
# package.
#======================================
add_subdirectory(zlog)
Loading

0 comments on commit 7fe61ca

Please sign in to comment.