├── .clang-format ├── .gitignore ├── BUILD.bazel ├── CMakeLists.txt ├── Changelog ├── INSTALL ├── LICENSE ├── Makefile ├── README.md ├── TODO ├── cmake ├── FindUnixem.cmake ├── LoadLibraries.cmake └── toolchain-mingw64.cmake ├── cpack ├── CMakeLists.txt ├── CPackConfig.cmake └── zlog │ ├── CMakeLists.txt │ ├── postinst │ ├── postrm │ ├── preinst │ └── prerm ├── doc ├── GettingStart-CN.txt ├── GettingStart-EN.txt ├── Makefile ├── UsersGuide-CN.lyx ├── UsersGuide-EN.lyx ├── performence.txt └── zlog.conf ├── src ├── CMakeLists.txt ├── Makefile ├── buf.c ├── buf.h ├── category.c ├── category.h ├── category_table.c ├── category_table.h ├── conf.c ├── conf.h ├── event.c ├── event.h ├── fmacros.h ├── format.c ├── format.h ├── level.c ├── level.h ├── level_list.c ├── level_list.h ├── lockfile.c ├── lockfile.h ├── mdc.c ├── mdc.h ├── record.c ├── record.h ├── record_table.c ├── record_table.h ├── rotater.c ├── rotater.h ├── rule.c ├── rule.h ├── spec.c ├── spec.h ├── thread.c ├── thread.h ├── version.h ├── zc_arraylist.c ├── zc_arraylist.h ├── zc_defs.h ├── zc_hashtable.c ├── zc_hashtable.h ├── zc_profile.c ├── zc_profile.h ├── zc_util.c ├── zc_util.h ├── zc_xplatform.h ├── zlog-chk-conf.c ├── zlog.c ├── zlog.h ├── zlog_win.c └── zlog_win.h ├── test ├── CMakeLists.txt ├── fuzzers │ └── zlog_init_fuzzer.c ├── hello_output ├── makefile ├── test_bitmap.c ├── test_buf.c ├── test_category.c ├── test_category.conf ├── test_conf.c ├── test_conf.conf ├── test_conf2.c ├── test_conf2.conf.h ├── test_default.c ├── test_default.conf ├── test_enabled.c ├── test_enabled.conf ├── test_enabled.h ├── test_hashtable.c ├── test_hello.c ├── test_hello.conf ├── test_hex.c ├── test_hex.conf ├── test_init.2.conf ├── test_init.c ├── test_init.conf ├── test_leak.2.conf ├── test_leak.c ├── test_leak.conf ├── test_level.c ├── test_level.conf ├── test_level.h ├── test_longlog.c ├── test_longlog.conf ├── test_mdc.c ├── test_mdc.conf ├── test_multithread.c ├── test_multithread.conf ├── test_pipe.c ├── test_pipe.conf ├── test_press_syslog.c ├── test_press_write.c ├── test_press_write2.c ├── test_press_zlog.c ├── test_press_zlog.conf ├── test_press_zlog2.c ├── test_press_zlog2.conf ├── test_profile.c ├── test_profile.conf ├── test_prompt.c ├── test_prompt.conf ├── test_record.c ├── test_record.conf ├── test_syslog.c ├── test_syslog.conf ├── test_tmp.c ├── test_tmp.conf └── val.sh ├── tools └── mk_targz.sh └── zlog.spec /.clang-format: -------------------------------------------------------------------------------- 1 | # Coding Style Proposal - Verify 2 | 3 | Language: Cpp 4 | BasedOnStyle: Microsoft 5 | 6 | ################################################ 7 | ReflowComments: false 8 | SortIncludes: false # can break build; turning off 9 | SortUsingDeclarations: false # can break build; turning off 10 | FixNamespaceComments: false 11 | 12 | BreakStringLiterals: false 13 | ColumnLimit: 100 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/c,c++,bazel,cmake,linux,macos,windows,jetbrains+all,visualstudiocode 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=c,c++,bazel,cmake,linux,macos,windows,jetbrains+all,visualstudiocode 3 | 4 | ### Bazel ### 5 | # gitignore template for Bazel build system 6 | # website: https://bazel.build/ 7 | 8 | # Ignore all bazel-* symlinks. There is no full list since this can change 9 | # based on the name of the directory bazel is cloned into. 10 | /bazel-* 11 | 12 | # Directories for the Bazel IntelliJ plugin containing the generated 13 | # IntelliJ project files and plugin configuration. Seperate directories are 14 | # for the IntelliJ, Android Studio and CLion versions of the plugin. 15 | /.ijwb/ 16 | /.aswb/ 17 | /.clwb/ 18 | 19 | ### C ### 20 | # Prerequisites 21 | *.d 22 | 23 | # Object files 24 | *.o 25 | *.ko 26 | *.obj 27 | *.elf 28 | 29 | # Linker output 30 | *.ilk 31 | *.map 32 | *.exp 33 | 34 | # Precompiled Headers 35 | *.gch 36 | *.pch 37 | 38 | # Libraries 39 | *.lib 40 | *.a 41 | *.la 42 | *.lo 43 | 44 | # Shared objects (inc. Windows DLLs) 45 | *.dll 46 | *.so 47 | *.so.* 48 | *.dylib 49 | 50 | # Executables 51 | *.exe 52 | *.out 53 | *.app 54 | *.i*86 55 | *.x86_64 56 | *.hex 57 | 58 | # Debug files 59 | *.dSYM/ 60 | *.su 61 | *.idb 62 | *.pdb 63 | 64 | # Kernel Module Compile Results 65 | *.mod* 66 | *.cmd 67 | .tmp_versions/ 68 | modules.order 69 | Module.symvers 70 | Mkfile.old 71 | dkms.conf 72 | 73 | ### C++ ### 74 | # Prerequisites 75 | 76 | # Compiled Object files 77 | *.slo 78 | 79 | # Precompiled Headers 80 | 81 | # Compiled Dynamic libraries 82 | 83 | # Fortran module files 84 | *.mod 85 | *.smod 86 | 87 | # Compiled Static libraries 88 | *.lai 89 | 90 | # Executables 91 | 92 | ### CMake ### 93 | CMakeLists.txt.user 94 | CMakeCache.txt 95 | CMakeFiles 96 | CMakeScripts 97 | Testing 98 | Makefile 99 | cmake_install.cmake 100 | install_manifest.txt 101 | compile_commands.json 102 | CTestTestfile.cmake 103 | _deps 104 | 105 | ### CMake Patch ### 106 | # External projects 107 | *-prefix/ 108 | 109 | ### JetBrains+all ### 110 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 111 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 112 | 113 | # User-specific stuff 114 | .idea/**/workspace.xml 115 | .idea/**/tasks.xml 116 | .idea/**/usage.statistics.xml 117 | .idea/**/dictionaries 118 | .idea/**/shelf 119 | 120 | # AWS User-specific 121 | .idea/**/aws.xml 122 | 123 | # Generated files 124 | .idea/**/contentModel.xml 125 | 126 | # Sensitive or high-churn files 127 | .idea/**/dataSources/ 128 | .idea/**/dataSources.ids 129 | .idea/**/dataSources.local.xml 130 | .idea/**/sqlDataSources.xml 131 | .idea/**/dynamic.xml 132 | .idea/**/uiDesigner.xml 133 | .idea/**/dbnavigator.xml 134 | 135 | # Gradle 136 | .idea/**/gradle.xml 137 | .idea/**/libraries 138 | 139 | # Gradle and Maven with auto-import 140 | # When using Gradle or Maven with auto-import, you should exclude module files, 141 | # since they will be recreated, and may cause churn. Uncomment if using 142 | # auto-import. 143 | # .idea/artifacts 144 | # .idea/compiler.xml 145 | # .idea/jarRepositories.xml 146 | # .idea/modules.xml 147 | # .idea/*.iml 148 | # .idea/modules 149 | # *.iml 150 | # *.ipr 151 | 152 | # CMake 153 | cmake-build-*/ 154 | 155 | # Mongo Explorer plugin 156 | .idea/**/mongoSettings.xml 157 | 158 | # File-based project format 159 | *.iws 160 | 161 | # IntelliJ 162 | out/ 163 | 164 | # mpeltonen/sbt-idea plugin 165 | .idea_modules/ 166 | 167 | # JIRA plugin 168 | atlassian-ide-plugin.xml 169 | 170 | # Cursive Clojure plugin 171 | .idea/replstate.xml 172 | 173 | # SonarLint plugin 174 | .idea/sonarlint/ 175 | 176 | # Crashlytics plugin (for Android Studio and IntelliJ) 177 | com_crashlytics_export_strings.xml 178 | crashlytics.properties 179 | crashlytics-build.properties 180 | fabric.properties 181 | 182 | # Editor-based Rest Client 183 | .idea/httpRequests 184 | 185 | # Android studio 3.1+ serialized cache file 186 | .idea/caches/build_file_checksums.ser 187 | 188 | ### JetBrains+all Patch ### 189 | # Ignore everything but code style settings and run configurations 190 | # that are supposed to be shared within teams. 191 | 192 | .idea/* 193 | 194 | !.idea/codeStyles 195 | !.idea/runConfigurations 196 | 197 | ### Linux ### 198 | *~ 199 | 200 | # temporary files which can be created if a process still has a handle open of a deleted file 201 | .fuse_hidden* 202 | 203 | # KDE directory preferences 204 | .directory 205 | 206 | # Linux trash folder which might appear on any partition or disk 207 | .Trash-* 208 | 209 | # .nfs files are created when an open file is removed but is still being accessed 210 | .nfs* 211 | 212 | ### macOS ### 213 | # General 214 | .DS_Store 215 | .AppleDouble 216 | .LSOverride 217 | 218 | # Icon must end with two \r 219 | Icon 220 | 221 | 222 | # Thumbnails 223 | ._* 224 | 225 | # Files that might appear in the root of a volume 226 | .DocumentRevisions-V100 227 | .fseventsd 228 | .Spotlight-V100 229 | .TemporaryItems 230 | .Trashes 231 | .VolumeIcon.icns 232 | .com.apple.timemachine.donotpresent 233 | 234 | # Directories potentially created on remote AFP share 235 | .AppleDB 236 | .AppleDesktop 237 | Network Trash Folder 238 | Temporary Items 239 | .apdisk 240 | 241 | ### macOS Patch ### 242 | # iCloud generated files 243 | *.icloud 244 | 245 | ### VisualStudioCode ### 246 | .vscode/* 247 | !.vscode/settings.json 248 | !.vscode/tasks.json 249 | !.vscode/launch.json 250 | !.vscode/extensions.json 251 | !.vscode/*.code-snippets 252 | 253 | # Local History for Visual Studio Code 254 | .history/ 255 | 256 | # Built Visual Studio Code Extensions 257 | *.vsix 258 | 259 | ### VisualStudioCode Patch ### 260 | # Ignore all local history of files 261 | .history 262 | .ionide 263 | 264 | ### Windows ### 265 | # Windows thumbnail cache files 266 | Thumbs.db 267 | Thumbs.db:encryptable 268 | ehthumbs.db 269 | ehthumbs_vista.db 270 | 271 | # Dump file 272 | *.stackdump 273 | 274 | # Folder config file 275 | [Dd]esktop.ini 276 | 277 | # Recycle Bin used on file shares 278 | $RECYCLE.BIN/ 279 | 280 | # Windows Installer files 281 | *.cab 282 | *.msi 283 | *.msix 284 | *.msm 285 | *.msp 286 | 287 | # Windows shortcuts 288 | *.lnk 289 | 290 | # build folder 291 | /build/ 292 | 293 | # End of https://www.toptal.com/developers/gitignore/api/c,c++,bazel,cmake,linux,macos,windows,jetbrains+all,visualstudiocode -------------------------------------------------------------------------------- /BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_cc//cc:defs.bzl", "cc_library") 2 | 3 | cc_library( 4 | name = "zlog", 5 | srcs = glob( 6 | [ 7 | "src/*.c", 8 | "src/*.h", 9 | ], 10 | exclude = [ 11 | "src/zlog_win.c", 12 | "src/zlog_win.h", 13 | ], 14 | ), 15 | hdrs = glob( 16 | [ 17 | "src/*.h", 18 | "*.h", 19 | ], 20 | exclude = [ 21 | "src/zlog_win.h", 22 | ], 23 | ), 24 | visibility = ["//visibility:public"], 25 | ) 26 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # LICENSE: Apache License 2.0 2 | # Copyright (c) Hardy Simpson 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | #/ 16 | 17 | cmake_minimum_required(VERSION 2.8.5) 18 | 19 | message(STATUS "path : ${CMAKE_FIND_ROOT_PATH}") 20 | project(zlog) 21 | message(STATUS "path : ${CMAKE_FIND_ROOT_PATH}") 22 | 23 | set(CMAKE_MODULE_PATH ${zlog_SOURCE_DIR}/cmake) 24 | 25 | SET(CPACK_PACKAGE_VERSION_MAJOR "1") 26 | SET(CPACK_PACKAGE_VERSION_MINOR "2") 27 | SET(CPACK_PACKAGE_VERSION_PATCH "18") 28 | SET(CPACK_RPM_PACKAGE_RELEASE 1) # release version. 29 | SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}") 30 | SET(ZLOG_VERSION ${CPACK_PACKAGE_VERSION}) 31 | SET(ZLOG_SO_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}) 32 | 33 | message(STATUS "platform : ${CMAKE_SYSTEM}") 34 | 35 | add_definitions("-g -Wall -Wstrict-prototypes") 36 | set(CMAKE_C_FLAGS "-std=c99 -pedantic -D_DEFAULT_SOURCE") 37 | set(CMAKE_C_FLAGS_DEBUG "-ggdb3 -DDEBUG") 38 | set(CMAKE_C_FLAGS_RELEASE "-O2") 39 | 40 | if (WIN32) 41 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWINVER=0x0500 -D_WIN32_WINNT=0x0500 ") 42 | endif () 43 | 44 | cmake_policy(SET CMP0015 NEW) 45 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/lib") 46 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/lib") 47 | 48 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/bin") 49 | 50 | link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) 51 | 52 | set(Need_THREAD 1) 53 | 54 | if (WIN32) 55 | set(Need_UNIXEM 1) 56 | endif () 57 | 58 | include(cmake/LoadLibraries.cmake) 59 | 60 | add_subdirectory(src) 61 | add_subdirectory(cpack) 62 | 63 | if (UNIT_TEST) 64 | enable_testing() 65 | add_subdirectory(test) 66 | endif () 67 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | README.md -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Top level makefile, the real shit is at src/makefile 2 | 3 | TARGETS=noopt 32bit 4 | 5 | all: 6 | cd src && $(MAKE) $@ 7 | 8 | install: 9 | cd src && $(MAKE) $@ 10 | 11 | $(TARGETS): 12 | cd src && $(MAKE) $@ 13 | 14 | doc: 15 | cd doc && $(MAKE) 16 | 17 | test: 18 | cd test && $(MAKE) 19 | 20 | TAGS: 21 | find . -type f -name "*.[ch]" | xargs etags - 22 | 23 | clean: 24 | cd src && $(MAKE) $@ 25 | cd test && $(MAKE) $@ 26 | cd doc && $(MAKE) $@ 27 | rm -f TAGS 28 | 29 | distclean: clean 30 | 31 | dummy: 32 | 33 | .PHONY: doc install test TAGS 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zlog 2 | 3 | zlog is a reliable, high-performance, thread safe, flexible, clear-model, pure C logging library. 4 | 5 | Actually, in the C world there was NO good logging library for applications like logback in java or log4cxx in c++. 6 | Using printf can work, but can not be redirected or reformatted easily. syslog is slow and is designed for system use. 7 | So I wrote zlog. 8 | It is faster, safer and more powerful than log4c. So it can be widely used. 9 | 10 | ## 1. Install 11 | 12 | Downloads: 13 | 14 | ```bash 15 | tar -zxvf zlog-latest-stable.tar.gz 16 | cd zlog-latest-stable/ 17 | make 18 | sudo make install 19 | ``` 20 | 21 | Or 22 | 23 | ```bash 24 | make PREFIX=/usr/local/ 25 | sudo make PREFIX=/usr/local/ install 26 | ``` 27 | 28 | PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure 29 | your program can find zlog library. 30 | 31 | ```bash 32 | $ sudo vi /etc/ld.so.conf 33 | /usr/local/lib 34 | $ sudo ldconfig 35 | ``` 36 | 37 | Before running a real program, make sure libzlog.so is in the directory where the system's dynamic lib loader can find 38 | it. The command metioned above are for linux. Other systems will need a similar set of actions. 39 | 40 | ## 2. Configuration file 41 | 42 | There are 3 important concepts in zlog: categories, formats and rules. 43 | 44 | Categories specify different kinds of log entries. In the zlog source code, category is a `zlog_category_t *` variable. 45 | In your program, different categories for the log entries will distinguish them from each other. 46 | 47 | Formats describe log patterns, such as: with or without time stamp, source file, source line. 48 | 49 | Rules consist of category, level, output file (or other channel) and format. In brief, if the category string in a rule 50 | in the configuration file equals the name of a category variable in the source, then they match. Still there is complex 51 | match range of category. Rule decouples variable conditions. For example, log4j must specify a level for each logger(or 52 | inherit from father logger). That's not convenient when each grade of logger has its own level for output(child logger 53 | output at the level of debug, when father logger output at the level of error) 54 | 55 | Now create a configuration file. The function zlog_init takes the files path as its only argument. 56 | 57 | ```bash 58 | $ cat /etc/zlog.conf 59 | 60 | [formats] 61 | simple = "%m%n" 62 | [rules] 63 | my_cat.DEBUG >stdout; simple 64 | ``` 65 | 66 | In the configuration file log messages in the category `my_cat` and a level of DEBUG or higher are output to standard 67 | output, with the format of simple `%m - usermessage %n - newline`. If you want to direct out to a file and limit the 68 | files maximum size, use this configuration 69 | 70 | ```config 71 | my_cat.DEBUG "/var/log/aa.log", 1M; simple 72 | ``` 73 | 74 | ## 3. Using zlog API in C source file 75 | 76 | ### 3.1 Sample code 77 | 78 | ```bash 79 | $ vi test_hello.c 80 | 81 | #include 82 | 83 | #include "zlog.h" 84 | 85 | int main(int argc, char** argv) 86 | { 87 | int rc; 88 | zlog_category_t *c; 89 | 90 | rc = zlog_init("/etc/zlog.conf"); 91 | if (rc) { 92 | printf("init failed\n"); 93 | return -1; 94 | } 95 | 96 | c = zlog_get_category("my_cat"); 97 | if (!c) { 98 | printf("get cat fail\n"); 99 | zlog_fini(); 100 | return -2; 101 | } 102 | 103 | zlog_info(c, "hello, zlog"); 104 | 105 | zlog_fini(); 106 | 107 | return 0; 108 | } 109 | ``` 110 | 111 | ### 3.2 Compile, and run it 112 | 113 | ```bash 114 | $ cc -c -o test_hello.o test_hello.c -I/usr/local/include 115 | $ cc -o test_hello test_hello.o -L/usr/local/lib -lzlog -lpthread 116 | $ ./test_hello 117 | hello, zlog 118 | ``` 119 | 120 | ## 4. Advanced Usage 121 | 122 | * syslog model, better than log4j model 123 | * log format customization 124 | * multiple output destinations including static file path, dynamic file path, stdout, stderr, syslog, user-defined 125 | output 126 | * runtime manually or automatically refresh configure(safely) 127 | * high-performance, 250'000 logs/second on my laptop, about 1000 times faster than syslog(3) with rsyslogd 128 | * user-defined log level 129 | * thread-safe and process-safe log file rotation 130 | * microsecond accuracy 131 | * dzlog, a default category log API for easy use 132 | * MDC, a log4j style key-value map 133 | * self debuggable, can output zlog's self debug&error log at runtime 134 | * No external dependencies, just based on a POSIX system and a C99 compliant vsnprintf. 135 | 136 | ## 5. Reference 137 | 138 | * Homepage: 139 | * Downloads: 140 | * Author's Email: 141 | * Auto tools version: 142 | * CMake verion: 143 | * Windows version: 144 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | [p] 使用valgrind测试性能 2 | [ ] 更好的错误展现,当系统出问题的时候直接报错 3 | [ ] hzlog的可定制 4 | [ ] hex那段重写,内置到buf内,参考od的设计 5 | [ ] 分类匹配的可定制化, rcat 6 | [ ] 自行管理文件缓存,替代stdio 7 | [ ] 减少dynamic文件名open的次数,通过日期改变智能推断, file_table? 8 | [ ] async file输出的增加 9 | [ ] 兼容性问题 zlog.h内 10 | [ ] 增加trace级别 11 | [ ] gettid() 12 | [ ] 性能对比, log4x, pantheios, glog 13 | [ ] perl, python, go, c++支持 14 | [ ] redis对接,协议设计 15 | [ ] 和rsyslog对接的问题 16 | [x] linux fsync->fdatasync, open.. 17 | 18 | -------------------------------------------------------------------------------- /cmake/FindUnixem.cmake: -------------------------------------------------------------------------------- 1 | # FindUnixem.cmake 2 | # author lsm 3 | 4 | if (NOT UNIXEM_FOUND) 5 | if (NOT UNIXEM_INCLUDE_DIR) 6 | find_path(UNIXEM_INCLUDE_DIR 7 | NAMES 8 | unixem/unixem.h 9 | ONLY_CMAKE_FIND_ROOT_PATH 10 | ) 11 | endif () 12 | 13 | if (NOT UNIXEM_LIBRARY) 14 | find_library(UNIXEM_LIBRARY 15 | NAMES unixem 16 | ONLY_CMAKE_FIND_ROOT_PATH 17 | ) 18 | endif (NOT UNIXEM_LIBRARY) 19 | 20 | message(STATUS " UNIXEM_INCLUDE_DIR = ${UNIXEM_INCLUDE_DIR}") 21 | message(STATUS " UNIXEM_LIBRARY = ${UNIXEM_LIBRARY}") 22 | 23 | if (UNIXEM_INCLUDE_DIR AND UNIXEM_LIBRARY) 24 | set(UNIXEM_FOUND TRUE) 25 | endif (UNIXEM_INCLUDE_DIR AND UNIXEM_LIBRARY) 26 | endif () 27 | -------------------------------------------------------------------------------- /cmake/LoadLibraries.cmake: -------------------------------------------------------------------------------- 1 | # ======================================================= 2 | # 支持多线程 3 | # 对于需要多线程的库,使用以下命令包含连接库: 4 | # target_link_libraries(xxx ${CMAKE_THREAD_PREFER_PTHREAD}) 5 | # ======================================================= 6 | if (Need_THREAD) 7 | find_package(Threads REQUIRED) 8 | 9 | if (NOT CMAKE_THREAD_PREFER_PTHREAD) 10 | set(CMAKE_THREAD_PREFER_PTHREAD ${CMAKE_THREAD_LIBS_INIT}) 11 | endif () 12 | 13 | message(STATUS "thread lib : ${CMAKE_THREAD_PREFER_PTHREAD}") 14 | endif (Need_THREAD) 15 | 16 | if (Need_UNIXEM) 17 | find_package(Unixem) 18 | 19 | if (NOT UNIXEM_FOUND) 20 | message(FATAL_ERROR "unixem lib not found!") 21 | endif () 22 | endif () 23 | -------------------------------------------------------------------------------- /cmake/toolchain-mingw64.cmake: -------------------------------------------------------------------------------- 1 | # this one is important 2 | SET(CMAKE_SYSTEM_NAME Windows) 3 | 4 | # specify the cross compiler 5 | find_path(MINGW_BIN_PATH gcc.exe PATHS c:/mingw64 d:/mingw64 c:/mingw-build/mingw64 d:/mingw-build/mingw64 PATH_SUFFIXES bin) 6 | 7 | if (MINGW_PATH_NOTFOUND) 8 | message(FATAL "mingw64 not found!") 9 | endif () 10 | 11 | get_filename_component(MINGW_PATH ${MINGW_BIN_PATH} PATH) 12 | 13 | SET(CMAKE_GENERATOR "MinGW Makefiles") 14 | 15 | SET(CMAKE_C_COMPILER gcc) 16 | SET(CMAKE_CXX_COMPILER g++) 17 | 18 | SET(CMAKE_RC_COMPILER windres) 19 | set(CMAKE_RC_COMPILE_OBJECT " -O coff -i -o ") 20 | 21 | # SET(_CMAKE_TOOLCHAIN_PREFIX x86_64-w64-mingw32-) 22 | SET(_CMAKE_TOOLCHAIN_LOCATION ${MINGW_BIN_PATH}) 23 | 24 | # where is the target environment 25 | SET(CMAKE_FIND_ROOT_PATH ${MINGW_PATH} ${MINGW_PATH}/x86_64-w64-mingw32) 26 | 27 | SET(CMAKE_SYSTEM_INCLUDE_PATH "${CMAKE_SYSTEM_INCLUDE_PATH} ${MINGW_PATH}/include ${MINGW_PATH}/x86_64-w64-mingw32/include") 28 | SET(CMAKE_SYSTEM_LIBRARY_PATH "${CMAKE_SYSTEM_LIBRARY_PATH} ${MINGW_PATH}/lib ${MINGW_PATH}/x86_64-w64-mingw32/lib") 29 | SET(CMAKE_SYSTEM_PROGRAM_PATH "${CMAKE_SYSTEM_PROGRAM_PATH} ${MINGW_PATH}/bin ${MINGW_PATH}/x86_64-w64-mingw32/bin") 30 | 31 | # printf support %sz format. 32 | add_definitions("-D_POSIX") 33 | 34 | # search for programs in the build host directories 35 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 36 | 37 | # for libraries and headers in the target directories 38 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 39 | SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 40 | -------------------------------------------------------------------------------- /cpack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # create by lsm 2 | 3 | SET(CPACK_PACKAGE_VENDOR "zlog") 4 | SET(CPACK_PACKAGE_CONTACT "HardySimpson1984@gmail.com") 5 | SET(CPACK_RPM_PACKAGE_LICENSE "Apache License Version 2.0") 6 | SET(CPACK_PACKAGING_INSTALL_PREFIX "/usr/local") 7 | SET(CPACK_RPM_PACKAGE_GROUP "System Environment/Base") 8 | 9 | message(STATUS "system process is ${CMAKE_HOST_SYSTEM_PROCESSOR}") 10 | message(STATUS "system process is ${CMAKE_SYSTEM_PROCESSOR}") 11 | 12 | IF (NOT CMAKE_SYSTEM_PROCESSOR) 13 | set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR}) 14 | endif () 15 | 16 | IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64|amd64") 17 | set(CMAKE_SYSTEM_PROCESSOR x86_64) 18 | SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE amd64) 19 | SET(CPACK_RPM_PACKAGE_ARCHITECTURE "x86_64") 20 | ELSEIF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc64|ppc64") 21 | SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE powerpc64) 22 | SET(CPACK_RPM_PACKAGE_ARCHITECTURE "ppc64") 23 | ELSEIF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc|ppc") 24 | SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE powerpc) 25 | SET(CPACK_RPM_PACKAGE_ARCHITECTURE "ppc") 26 | ELSE () 27 | SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386) 28 | SET(CPACK_RPM_PACKAGE_ARCHITECTURE i386) 29 | ENDIF () 30 | 31 | SET(CPACK_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") 32 | SET(CPACK_TOPLEVEL_TAG "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}") 33 | 34 | SET(CPACK_CMAKE_GENERATOR "Unix Makefiles") 35 | 36 | if (WIN32) 37 | SET(CPACK_GENERATOR "NSIS") 38 | else () 39 | SET(CPACK_GENERATOR "RPM;DEB") 40 | endif () 41 | 42 | add_subdirectory(zlog) 43 | -------------------------------------------------------------------------------- /cpack/CPackConfig.cmake: -------------------------------------------------------------------------------- 1 | SET(CPACK_CMAKE_GENERATOR "${CPACK_CMAKE_GENERATOR}") 2 | SET(CPACK_GENERATOR "${CPACK_GENERATOR}") 3 | SET(CPACK_OUTPUT_CONFIG_FILE "${CPACK_OUTPUT_CONFIG_FILE}") 4 | SET(CPACK_INSTALL_CMAKE_PROJECTS "${CPACK_INSTALL_CMAKE_PROJECTS}") 5 | 6 | SET(CPACK_PACKAGE_VERSION_MAJOR "${CPACK_PACKAGE_VERSION_MAJOR}") 7 | SET(CPACK_PACKAGE_VERSION_MINOR "${CPACK_PACKAGE_VERSION_MINOR}") 8 | SET(CPACK_PACKAGE_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH}") 9 | SET(CPACK_RPM_PACKAGE_RELEASE "${CPACK_RPM_PACKAGE_RELEASE}") # release version. 10 | SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}") 11 | 12 | SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}") 13 | SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${CPACK_PACKAGE_DESCRIPTION_SUMMARY}") 14 | SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CPACK_PACKAGE_DESCRIPTION_FILE}") 15 | 16 | SET(CPACK_PACKAGE_VENDOR "${CPACK_PACKAGE_VENDOR}") 17 | SET(CPACK_PACKAGING_INSTALL_PREFIX "${CPACK_PACKAGING_INSTALL_PREFIX}") 18 | SET(CPACK_PACKAGE_CONTACT "${CPACK_PACKAGE_CONTACT}") 19 | 20 | SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "${CPACK_PACKAGE_NAME} ${CPACK_PACKAGE_VERSION}") 21 | 22 | SET(CPACK_SYSTEM_NAME "${CPACK_SYSTEM_NAME}") 23 | SET(CPACK_TOPLEVEL_TAG "${CPACK_TOPLEVEL_TAG}") 24 | SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}-${CPACK_TOPLEVEL_TAG}") 25 | 26 | # SET(CPACK_PACKAGE_EXECUTABLES "ccmake;CMake") 27 | # SET(CPACK_STRIP_FILES "bin/ccmake;bin/cmake;bin/cpack;bin/ctest") 28 | set(CPACK_NSIS_MODIFY_PATH, ON) 29 | 30 | SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}") 31 | SET(CPACK_RPM_PACKAGE_ARCHITECTURE "${CPACK_RPM_PACKAGE_ARCHITECTURE}") 32 | 33 | SET(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES}") 34 | SET(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}") 35 | 36 | SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA 37 | "${CMAKE_CURRENT_BINARY_DIR}/preinst;${CMAKE_CURRENT_BINARY_DIR}/postinst;${CMAKE_CURRENT_BINARY_DIR}/prerm;${CMAKE_CURRENT_BINARY_DIR}/postrm") 38 | 39 | SET(CPACK_RPM_PRE_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/preinst") 40 | SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/postinst") 41 | SET(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/prerm") 42 | SET(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/postrm") 43 | 44 | SET(CPACK_RPM_PACKAGE_LICENSE "${CPACK_RPM_PACKAGE_LICENSE}") 45 | 46 | SET(CPACK_RPM_PACKAGE_GROUP "${CPACK_RPM_PACKAGE_GROUP}") 47 | -------------------------------------------------------------------------------- /cpack/zlog/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # create by lsm 2 | 3 | # =============================== 4 | # package info setting 5 | # =============================== 6 | SET(CPACK_PACKAGE_NAME "zlog") 7 | SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "log component for Linux/Unix/AIX") 8 | SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_HOME_DIRECTORY}/README") 9 | SET(CPACK_INSTALL_CMAKE_PROJECTS "${zlog_BINARY_DIR};zlog;zlog;/") 10 | 11 | # ================================= 12 | # dependency setting 13 | # ================================= 14 | # SET(CPACK_RPM_PACKAGE_REQUIRES "") 15 | # SET(CPACK_DEBIAN_PACKAGE_DEPENDS "") 16 | 17 | # =============================== 18 | # copy file to build directory. 19 | # =============================== 20 | SET(CPACK_OUTPUT_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/CPackConfig.cmake") 21 | configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../CPackConfig.cmake CPackConfig.cmake) 22 | 23 | file(COPY 24 | . 25 | 26 | DESTINATION ${CMAKE_CURRENT_BINARY_DIR} 27 | 28 | PATTERN CMakeLists.txt EXCLUDE 29 | ) 30 | -------------------------------------------------------------------------------- /cpack/zlog/postinst: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ldconfig 4 | 5 | -------------------------------------------------------------------------------- /cpack/zlog/postrm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ldconfig 4 | 5 | 6 | -------------------------------------------------------------------------------- /cpack/zlog/preinst: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | -------------------------------------------------------------------------------- /cpack/zlog/prerm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | -------------------------------------------------------------------------------- /doc/GettingStart-CN.txt: -------------------------------------------------------------------------------- 1 | 0. zlog是什么? 2 | 3 | zlog是一个高可靠性、高性能、线程安全、灵活、概念清晰的纯C日志函数库。 4 | 5 | 事实上,在C的世界里面没有特别好的日志函数库(就像JAVA里面的的log4j,或者C++的log4cxx)。C程序员都喜欢用自己的轮子。printf就是个挺好的轮子,但没办法通过配置改变日志的格式或者输出文件。syslog是个系统级别的轮子,不过速度慢,而且功能比较单调。 6 | 所以我写了zlog。 7 | zlog在效率、功能、安全性上大大超过了log4c,并且是用c写成的,具有比较好的通用性。 8 | 9 | 1.安装 10 | 11 | 下载:https://github.com/HardySimpson/zlog/releases 12 | 13 | $ tar -zxvf zlog-latest-stable.tar.gz 14 | $ cd zlog-latest-stable/ 15 | $ make 16 | $ sudo make install 17 | or 18 | $ make PREFIX=/usr/local/ 19 | $ sudo make PREFIX=/usr/local/ install 20 | 21 | PREFIX指明了安装的路径,安转完之后为了让你的程序能找到zlog动态库 22 | 23 | $ sudo vi /etc/ld.so.conf 24 | /usr/local/lib 25 | $ sudo ldconfig 26 | 27 | 在你的程序运行之前,保证libzlog.so在系统的动态链接库加载器可以找到的目录下。上面的命令适用于linux,别的系统自己想办法。 28 | 29 | 30 | 2.介绍一下配置文件 31 | 32 | zlog里面有三个重要的概念,category,format,rule 33 | 34 | 分类(Category)用于区分不同的输入,代码中的分类变量的名字是一个字符串,在一个程序里面可以通过获取不同的分类名的category用来后面输出不同分类的日志,用于不同的目的。 35 | 36 | 格式(Format)是用来描述输出日志的格式,比如是否有带有时间戳, 是否包含文件位置信息等,上面的例子里面的格式simple就配置成简单的用户输入的信息+换行符。 37 | 38 | 规则(Rule)则是把分类、级别、输出文件、格式组合起来,决定一条代码中的日志是否输出,输出到哪里,以什么格式输出。简单而言,规则里面的分类字符串和代码里面的分类变量的名字一样就匹配,当然还有更高级的纲目分类匹配。规则彻底解耦了各个元素之间的强绑定,例如log4j就必须为每个分类指定一个级别(或者从父分类那里继承),这在多层系统需要每一层都有自己的级别要求的时候非常不方便。 39 | 40 | 现在试着写配置文件,配置文件名无所谓,放在哪里也无所谓,反正在zlog_init()的时候可以指定 41 | $ cat /etc/zlog.conf 42 | 43 | [formats] 44 | simple = "%m%n" 45 | [rules] 46 | my_cat.DEBUG >stdout; simple 47 | 48 | 在目前的配置文件的例子里面,可以看到my_cat分类,>=debug等级的日志会被输出到stdout(标准输出),并且输出的格式是simple这个格式,也就是用户输入信息+换行符。如果要输出到文件并控制文件大小为1兆,规则的配置应该是 49 | my_cat.DEBUG "/var/log/aa.log", 1M; simple 50 | 51 | 3.在代码中使用 52 | $ vi test_hello.c 53 | 54 | #include 55 | 56 | #include "zlog.h" 57 | 58 | int main(int argc, char** argv) 59 | { 60 | int rc; 61 | zlog_category_t *c; 62 | 63 | rc = zlog_init("/etc/zlog.conf"); 64 | if (rc) { 65 | printf("init failed\n"); 66 | return -1; 67 | } 68 | 69 | c = zlog_get_category("my_cat"); 70 | if (!c) { 71 | printf("get cat fail\n"); 72 | zlog_fini(); 73 | return -2; 74 | } 75 | 76 | zlog_info(c, "hello, zlog"); 77 | 78 | zlog_fini(); 79 | 80 | return 0; 81 | } 82 | 83 | 4.编译、然后运行! 84 | 85 | $ cc -c -o test_hello.o test_hello.c -I/usr/local/include 86 | $ cc -o test_hello test_hello.o -L/usr/local/lib -lzlog -lpthread 87 | $ ./test_hello 88 | hello, zlog 89 | 90 | 5.高级使用 91 | * syslog分类模型,比log4j模型更加直接了当 92 | * 日志格式定制,类似于log4j的pattern layout 93 | * 多种输出,包括动态文件、静态文件、stdout、stderr、syslog、用户自定义输出函数 94 | * 运行时手动、自动刷新配置文件(同时保证安全) 95 | * 高性能,在我的笔记本上达到25万条日志每秒, 大概是syslog(3)配合rsyslogd的1000倍速度 96 | * 用户自定义等级 97 | * 多线程和多进程环境下保证安全转档 98 | * 精确到微秒 99 | * 简单调用包装dzlog(一个程序默认只用一个分类) 100 | * MDC,线程键-值对的表,可以扩展用户自定义的字段 101 | * 自诊断,可以在运行时输出zlog自己的日志和配置状态 102 | * 不依赖其他库,只要是个POSIX系统就成(当然还要一个C99兼容的vsnprintf) 103 | 104 | 6.相关链接 105 | 主页:http://hardysimpson.github.com/zlog/ 106 | 软件下载:https://github.com/HardySimpson/zlog/releases 107 | 作者邮箱:HardySimpson1984@gmail.com 108 | 109 | auto tools版本: https://github.com/bmanojlovic/zlog 110 | cmake版本: https://github.com/lisongmin/zlog 111 | windows版本: https://github.com/lopsd07/WinZlog 112 | -------------------------------------------------------------------------------- /doc/GettingStart-EN.txt: -------------------------------------------------------------------------------- 1 | 0. What is zlog? 2 | 3 | zlog is a reliable, high-performance, thread safe, flexible, clear-model, pure C logging library. 4 | 5 | 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. 6 | So I wrote zlog. 7 | It is faster, safer and more powerful than log4c. So it can be widely used. 8 | 9 | 1. Install 10 | 11 | Downloads: https://github.com/HardySimpson/zlog/releases 12 | 13 | $ tar -zxvf zlog-latest-stable.tar.gz 14 | $ cd zlog-latest-stable/ 15 | $ make 16 | $ sudo make install 17 | or 18 | $ make PREFIX=/usr/local/ 19 | $ sudo make PREFIX=/usr/local/ install 20 | 21 | PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure your program can find zlog library. 22 | 23 | $ sudo vi /etc/ld.so.conf 24 | /usr/local/lib 25 | $ sudo ldconfig 26 | 27 | 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. 28 | 29 | 30 | 2. Introduce configure file 31 | 32 | There are 3 important concepts in zlog: categories, formats and rules. 33 | 34 | 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. 35 | 36 | Formats describe log patterns, such as: with or without time stamp, source file, source line. 37 | 38 | 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) 39 | 40 | Now create a configuration file. The function zlog_init takes the files path as its only argument. 41 | $ cat /etc/zlog.conf 42 | 43 | [formats] 44 | simple = "%m%n" 45 | [rules] 46 | my_cat.DEBUG >stdout; simple 47 | 48 | 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 49 | 50 | my_cat.DEBUG "/var/log/aa.log", 1M; simple 51 | 52 | 3. Using zlog API in C source file 53 | $ vi test_hello.c 54 | 55 | #include 56 | 57 | #include "zlog.h" 58 | 59 | int main(int argc, char** argv) 60 | { 61 | int rc; 62 | zlog_category_t *c; 63 | 64 | rc = zlog_init("/etc/zlog.conf"); 65 | if (rc) { 66 | printf("init failed\n"); 67 | return -1; 68 | } 69 | 70 | c = zlog_get_category("my_cat"); 71 | if (!c) { 72 | printf("get cat fail\n"); 73 | zlog_fini(); 74 | return -2; 75 | } 76 | 77 | zlog_info(c, "hello, zlog"); 78 | 79 | zlog_fini(); 80 | 81 | return 0; 82 | } 83 | 84 | 4. Complie, and run it!s 85 | 86 | $ cc -c -o test_hello.o test_hello.c -I/usr/local/include 87 | $ cc -o test_hello test_hello.o -L/usr/local/lib -lzlog -lpthread 88 | $ ./test_hello 89 | hello, zlog 90 | 91 | 5. Advanced Usage 92 | * syslog model, better than log4j model 93 | * log format customization 94 | * multiple output destinations including static file path, dynamic file path, stdout, stderr, syslog, user-defined ouput 95 | * runtime manually or automatically refresh configure(safely) 96 | * high-performance, 250'000 logs/second on my laptop, about 1000 times faster than syslog(3) with rsyslogd 97 | * user-defined log level 98 | * thread-safe and process-safe log file rotation 99 | * microsecond accuracy 100 | * dzlog, a default category log API for easy use 101 | * MDC, a log4j style key-value map 102 | * self debuggable, can output zlog's self debug&error log at runtime 103 | * No external dependencies, just based on a POSIX system and a C99 compliant vsnprintf. 104 | 105 | 106 | 6. Links: 107 | 108 | Homepage: http://hardysimpson.github.com/zlog 109 | Downloads: https://github.com/HardySimpson/zlog/releases 110 | Author's Email: HardySimpson1984@gmail.com 111 | 112 | auto tools version: https://github.com/bmanojlovic/zlog 113 | cmake verion: https://github.com/lisongmin/zlog 114 | windows version: https://github.com/lopsd07/WinZlog 115 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | doc = \ 2 | UsersGuide-EN.pdf \ 3 | UsersGuide-CN.pdf \ 4 | UsersGuide-EN.tex \ 5 | UsersGuide-CN.tex \ 6 | UsersGuide-EN.html \ 7 | UsersGuide-CN.html \ 8 | GettingStart-EN.txt \ 9 | GettingStart-CN.txt 10 | 11 | all : $(doc) 12 | 13 | UsersGuide-EN.pdf : UsersGuide-EN.lyx 14 | lyx -f -e pdf2 $^ 15 | 16 | UsersGuide-CN.pdf : UsersGuide-CN.lyx 17 | lyx -f -e pdf4 $^ 18 | 19 | %.tex : %.lyx 20 | lyx -f -e pdflatex $< 21 | 22 | %.html : %.tex 23 | hevea book.hva -s $< 24 | hevea book.hva -s $< 25 | 26 | clean : 27 | -rm -f *.pdf *.haux *.html *.htoc *.tex *.lyx~ 28 | -------------------------------------------------------------------------------- /doc/performence.txt: -------------------------------------------------------------------------------- 1 | ------------------------------------------------- 2 | using makefile.linux for test, libzlog compile in O2 3 | ------------------------------------------------- 4 | [direct write, no logging library] - The Sky! 5 | $ time ./test_press_write 1 10 100000 6 | real 0m1.872s 7 | user 0m0.140s 8 | sys 0m3.290s 9 | 10 | $ time ./test_press_write2 1 10 100000 11 | real 0m0.909s 12 | user 0m0.080s 13 | sys 0m1.710s 14 | ------------------------------------------------- 15 | v1.1.1 (fwrite is not atomic, so backup to write) 16 | $ time ./test_press_zlog 1 10 100000 17 | real 0m2.334s 18 | user 0m1.780s 19 | sys 0m2.710s 20 | 21 | $ time ./test_press_zlog2 1 10 100000 22 | real 0m2.134s 23 | user 0m1.840s 24 | sys 0m1.990s 25 | ------------------------------------------------- 26 | v1.1.0 27 | $ time ./test_press_zlog 1 10 100000 28 | real 0m1.107s 29 | user 0m1.500s 30 | sys 0m0.340s 31 | 32 | $ time ./test_press_zlog2 1 10 100000 33 | real 0m0.898s 34 | user 0m1.480s 35 | sys 0m0.150s 36 | ------------------------------------------------- 37 | v1.0.7 38 | $ time ./test_press_zlog 1 10 100000 39 | real 0m1.783s 40 | user 0m3.000s 41 | sys 0m0.300s 42 | 43 | $ time ./test_press_zlog2 1 10 100000 44 | real 0m1.621s 45 | user 0m2.980s 46 | sys 0m0.120s 47 | ------------------------------------------------- 48 | v1.0.6 49 | $ time ./test_press_zlog 1 10 100000 50 | real 0m1.814s 51 | user 0m3.060s 52 | sys 0m0.270s 53 | 54 | $ time ./test_press_zlog2 1 10 100000 55 | real 0m1.605s 56 | user 0m2.990s 57 | sys 0m0.140s 58 | ------------------------------------------------- 59 | v1.0.5 60 | $ time ./test_press_zlog 1 10 100000 61 | real 0m2.779s 62 | user 0m4.170s 63 | sys 0m0.560s 64 | 65 | $ time ./test_press_zlog2 1 10 100000 66 | real 0m2.713s 67 | user 0m4.410s 68 | sys 0m0.900s 69 | ------------------------------------------------- 70 | v1.0.3 71 | $ time ./test_press_zlog 1 10 100000 72 | 73 | real 0m6.349s 74 | user 0m6.300s 75 | sys 0m5.950s 76 | 77 | $ time ./test_press_zlog2 1 10 100000 78 | real 0m6.377s 79 | user 0m6.240s 80 | sys 0m6.090s 81 | ------------------------------------------------- 82 | v1.0.0 83 | $ time ./test_press_zlog 1 10 100000 84 | real 0m6.364s 85 | user 0m6.040s 86 | sys 0m6.270s 87 | 88 | $ time ./test_press_zlog2 1 10 100000 89 | real 0m6.361s 90 | user 0m6.150s 91 | sys 0m6.020s 92 | ------------------------------------------------- 93 | -------------------------------------------------------------------------------- /doc/zlog.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | reload conf period = 10M 4 | 5 | buffer min = 1024 6 | buffer max = 2MB 7 | 8 | #rotate lock file = /tmp/zlog.lock 9 | rotate lock file = self 10 | default format = "%d(%F %T.%l) %-6V (%c:%F:%L) - %m%n" 11 | 12 | file perms = 600 13 | fsync period = 1K 14 | 15 | [levels] 16 | TRACE = 10 17 | CRIT = 130, LOG_CRIT 18 | 19 | [formats] 20 | simple = "%m%n" 21 | normal = "%d(%F %T.%l) %m%n" 22 | 23 | [rules] 24 | default.* >stdout; simple 25 | 26 | *.* -"%12.2E(HOME)/log/%c.log", \ 27 | 1MB * 12 ~ "%E(HOME)/log/%c.%D(%F) #2r #3s.log"; \ 28 | simple 29 | 30 | my_.INFO >stderr; 31 | my_cat.!ERROR "aa.log" 32 | my_dog.=DEBUG >syslog, LOG_LOCAL0; simple 33 | my_dog.=DEBUG | /usr/bin/cronolog /www/logs/example_%Y%m%d.log ; normal 34 | my_mice.* $record_func , "record_path%c"; normal 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | aux_source_directory(. SRCS) 2 | 3 | if (NOT WIN32) 4 | list(REMOVE_ITEM SRCS ./zlog_win.c) 5 | endif () 6 | 7 | list(REMOVE_ITEM SRCS ./zlog-chk-conf.c) 8 | 9 | add_library(zlog 10 | SHARED 11 | ${SRCS} 12 | ) 13 | target_link_libraries(zlog 14 | ${CMAKE_THREAD_PREFER_PTHREAD} 15 | ) 16 | 17 | if (WIN32) 18 | target_link_libraries(zlog 19 | ${UNIXEM_LIBRARY} 20 | Ws2_32 21 | ) 22 | endif () 23 | 24 | set_target_properties(zlog PROPERTIES VERSION ${ZLOG_VERSION} SOVERSION ${ZLOG_SO_VERSION}) 25 | 26 | add_library(zlog_s 27 | STATIC 28 | ${SRCS} 29 | ) 30 | target_link_libraries(zlog_s 31 | ${CMAKE_THREAD_PREFER_PTHREAD} 32 | ) 33 | 34 | if (WIN32) 35 | target_link_libraries(zlog_s 36 | ${UNIXEM_LIBRARY} 37 | Ws2_32 38 | ) 39 | endif () 40 | 41 | set_target_properties(zlog_s PROPERTIES OUTPUT_NAME zlog) 42 | 43 | add_executable(zlog-chk-conf zlog-chk-conf.c) 44 | target_link_libraries(zlog-chk-conf zlog) 45 | 46 | install(TARGETS 47 | zlog zlog_s zlog-chk-conf 48 | COMPONENT zlog 49 | ARCHIVE DESTINATION lib 50 | LIBRARY DESTINATION lib 51 | RUNTIME DESTINATION bin 52 | ) 53 | 54 | install(FILES 55 | zlog.h 56 | COMPONENT zlog 57 | DESTINATION include 58 | ) 59 | -------------------------------------------------------------------------------- /src/buf.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_buf_h 17 | #define __zlog_buf_h 18 | 19 | /* buf, is a dynamic expand buffer for one single log, 20 | * as one single log will interlace if use multiple write() to file. 21 | * and buf is always keep in a thread, to make each thread has its 22 | * own buffer to avoid lock. 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | typedef struct zlog_buf_s { 29 | char *start; 30 | char *tail; 31 | char *end; 32 | char *end_plus_1; 33 | 34 | size_t size_min; 35 | size_t size_max; 36 | size_t size_real; 37 | 38 | char truncate_str[MAXLEN_PATH + 1]; 39 | size_t truncate_str_len; 40 | } zlog_buf_t; 41 | 42 | 43 | zlog_buf_t *zlog_buf_new(size_t min, size_t max, const char *truncate_str); 44 | void zlog_buf_del(zlog_buf_t * a_buf); 45 | void zlog_buf_profile(zlog_buf_t * a_buf, int flag); 46 | 47 | int zlog_buf_vprintf(zlog_buf_t * a_buf, const char *format, va_list args); 48 | int zlog_buf_append(zlog_buf_t * a_buf, const char *str, size_t str_len); 49 | int zlog_buf_adjust_append(zlog_buf_t * a_buf, const char *str, size_t str_len, 50 | int left_adjust, int zero_pad, size_t in_width, size_t out_width); 51 | int zlog_buf_printf_dec32(zlog_buf_t * a_buf, uint32_t ui32, int width); 52 | int zlog_buf_printf_dec64(zlog_buf_t * a_buf, uint64_t ui64, int width); 53 | int zlog_buf_printf_hex(zlog_buf_t * a_buf, uint32_t ui32, int width); 54 | 55 | #define zlog_buf_restart(a_buf) do { \ 56 | a_buf->tail = a_buf->start; \ 57 | } while(0) 58 | 59 | #define zlog_buf_len(a_buf) (a_buf->tail - a_buf->start) 60 | #define zlog_buf_str(a_buf) (a_buf->start) 61 | #define zlog_buf_seal(a_buf) do {*(a_buf)->tail = '\0';} while (0) 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /src/category.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "fmacros.h" 16 | #include 17 | #include 18 | #include 19 | 20 | #include "category.h" 21 | #include "rule.h" 22 | #include "zc_defs.h" 23 | 24 | void zlog_category_profile(zlog_category_t *a_category, int flag) 25 | { 26 | int i; 27 | zlog_rule_t *a_rule; 28 | 29 | zc_assert(a_category,); 30 | zc_profile(flag, "--category[%p][%s][%p]--", 31 | a_category, 32 | a_category->name, 33 | a_category->fit_rules); 34 | if (a_category->fit_rules) { 35 | zc_arraylist_foreach(a_category->fit_rules, i, a_rule) { 36 | zlog_rule_profile(a_rule, flag); 37 | } 38 | } 39 | return; 40 | } 41 | 42 | /*******************************************************************************/ 43 | void zlog_category_del(zlog_category_t * a_category) 44 | { 45 | zc_assert(a_category,); 46 | if (a_category->fit_rules) zc_arraylist_del(a_category->fit_rules); 47 | zc_debug("zlog_category_del[%p]", a_category); 48 | free(a_category); 49 | return; 50 | } 51 | 52 | /* overlap one rule's level bitmap to cateogry, 53 | * so category can judge whether a log level will be output by itself 54 | * It is safe when configure is reloaded, when rule will be released an recreated 55 | */ 56 | static void zlog_cateogry_overlap_bitmap(zlog_category_t * a_category, zlog_rule_t *a_rule) 57 | { 58 | int i; 59 | for(i = 0; i < sizeof(a_rule->level_bitmap); i++) { 60 | a_category->level_bitmap[i] |= a_rule->level_bitmap[i]; 61 | } 62 | } 63 | 64 | static int zlog_category_obtain_rules(zlog_category_t * a_category, zc_arraylist_t * rules) 65 | { 66 | int i; 67 | int count = 0; 68 | int fit = 0; 69 | zlog_rule_t *a_rule; 70 | zlog_rule_t *wastebin_rule = NULL; 71 | 72 | /* before set, clean last fit rules first */ 73 | if (a_category->fit_rules) zc_arraylist_del(a_category->fit_rules); 74 | 75 | memset(a_category->level_bitmap, 0x00, sizeof(a_category->level_bitmap)); 76 | 77 | a_category->fit_rules = zc_arraylist_new(NULL); 78 | if (!(a_category->fit_rules)) { 79 | zc_error("zc_arraylist_new fail"); 80 | return -1; 81 | } 82 | 83 | /* get match rules from all rules */ 84 | zc_arraylist_foreach(rules, i, a_rule) { 85 | fit = zlog_rule_match_category(a_rule, a_category->name); 86 | if (fit) { 87 | if (zc_arraylist_add(a_category->fit_rules, a_rule)) { 88 | zc_error("zc_arrylist_add fail"); 89 | goto err; 90 | } 91 | zlog_cateogry_overlap_bitmap(a_category, a_rule); 92 | count++; 93 | } 94 | 95 | if (zlog_rule_is_wastebin(a_rule)) { 96 | wastebin_rule = a_rule; 97 | } 98 | } 99 | 100 | if (count == 0) { 101 | if (wastebin_rule) { 102 | zc_debug("category[%s], no match rules, use wastebin_rule", a_category->name); 103 | if (zc_arraylist_add(a_category->fit_rules, wastebin_rule)) { 104 | zc_error("zc_arrylist_add fail"); 105 | goto err; 106 | } 107 | zlog_cateogry_overlap_bitmap(a_category, wastebin_rule); 108 | count++; 109 | } else { 110 | zc_debug("category[%s], no match rules & no wastebin_rule", a_category->name); 111 | } 112 | } 113 | 114 | return 0; 115 | err: 116 | zc_arraylist_del(a_category->fit_rules); 117 | a_category->fit_rules = NULL; 118 | return -1; 119 | } 120 | 121 | zlog_category_t *zlog_category_new(const char *name, zc_arraylist_t * rules) 122 | { 123 | size_t len; 124 | zlog_category_t *a_category; 125 | 126 | zc_assert(name, NULL); 127 | zc_assert(rules, NULL); 128 | 129 | len = strlen(name); 130 | if (len > sizeof(a_category->name) - 1) { 131 | zc_error("name[%s] too long", name); 132 | return NULL; 133 | } 134 | a_category = calloc(1, sizeof(zlog_category_t)); 135 | if (!a_category) { 136 | zc_error("calloc fail, errno[%d]", errno); 137 | return NULL; 138 | } 139 | strcpy(a_category->name, name); 140 | a_category->name_len = len; 141 | if (zlog_category_obtain_rules(a_category, rules)) { 142 | zc_error("zlog_category_fit_rules fail"); 143 | goto err; 144 | } 145 | 146 | zlog_category_profile(a_category, ZC_DEBUG); 147 | return a_category; 148 | err: 149 | zlog_category_del(a_category); 150 | return NULL; 151 | } 152 | /*******************************************************************************/ 153 | /* update success: fit_rules 1, fit_rules_backup 1 */ 154 | /* update fail: fit_rules 0, fit_rules_backup 1 */ 155 | int zlog_category_update_rules(zlog_category_t * a_category, zc_arraylist_t * new_rules) 156 | { 157 | zc_assert(a_category, -1); 158 | zc_assert(new_rules, -1); 159 | 160 | /* 1st, mv fit_rules fit_rules_backup */ 161 | if (a_category->fit_rules_backup) zc_arraylist_del(a_category->fit_rules_backup); 162 | a_category->fit_rules_backup = a_category->fit_rules; 163 | a_category->fit_rules = NULL; 164 | 165 | memcpy(a_category->level_bitmap_backup, a_category->level_bitmap, 166 | sizeof(a_category->level_bitmap)); 167 | 168 | /* 2nd, obtain new_rules to fit_rules */ 169 | if (zlog_category_obtain_rules(a_category, new_rules)) { 170 | zc_error("zlog_category_obtain_rules fail"); 171 | a_category->fit_rules = NULL; 172 | return -1; 173 | } 174 | 175 | /* keep the fit_rules_backup not change, return */ 176 | return 0; 177 | } 178 | 179 | /* commit fail: fit_rules_backup != 0 */ 180 | /* commit success: fit_rules 1, fit_rules_backup 0 */ 181 | void zlog_category_commit_rules(zlog_category_t * a_category) 182 | { 183 | zc_assert(a_category,); 184 | if (!a_category->fit_rules_backup) { 185 | zc_warn("a_category->fit_rules_backup is NULL, never update before"); 186 | return; 187 | } 188 | 189 | zc_arraylist_del(a_category->fit_rules_backup); 190 | a_category->fit_rules_backup = NULL; 191 | memset(a_category->level_bitmap_backup, 0x00, 192 | sizeof(a_category->level_bitmap_backup)); 193 | return; 194 | } 195 | 196 | /* rollback fail: fit_rules_backup != 0 */ 197 | /* rollback success: fit_rules 1, fit_rules_backup 0 */ 198 | /* so whether update succes or not, make things back to old */ 199 | void zlog_category_rollback_rules(zlog_category_t * a_category) 200 | { 201 | zc_assert(a_category,); 202 | if (!a_category->fit_rules_backup) { 203 | zc_warn("a_category->fit_rules_backup in NULL, never update before"); 204 | return; 205 | } 206 | 207 | if (a_category->fit_rules) { 208 | /* update success, rm new and backup */ 209 | zc_arraylist_del(a_category->fit_rules); 210 | a_category->fit_rules = a_category->fit_rules_backup; 211 | a_category->fit_rules_backup = NULL; 212 | } else { 213 | /* update fail, just backup */ 214 | a_category->fit_rules = a_category->fit_rules_backup; 215 | a_category->fit_rules_backup = NULL; 216 | } 217 | 218 | memcpy(a_category->level_bitmap, a_category->level_bitmap_backup, 219 | sizeof(a_category->level_bitmap)); 220 | memset(a_category->level_bitmap_backup, 0x00, 221 | sizeof(a_category->level_bitmap_backup)); 222 | 223 | return; /* always success */ 224 | } 225 | 226 | /*******************************************************************************/ 227 | 228 | int zlog_category_output(zlog_category_t * a_category, zlog_thread_t * a_thread) 229 | { 230 | int i; 231 | int rc = 0; 232 | zlog_rule_t *a_rule; 233 | 234 | /* go through all match rules to output */ 235 | zc_arraylist_foreach(a_category->fit_rules, i, a_rule) { 236 | rc = zlog_rule_output(a_rule, a_thread); 237 | } 238 | 239 | return rc; 240 | } 241 | -------------------------------------------------------------------------------- /src/category.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_category_h 17 | #define __zlog_category_h 18 | 19 | #include "zc_defs.h" 20 | #include "thread.h" 21 | 22 | typedef struct zlog_category_s { 23 | char name[MAXLEN_PATH + 1]; 24 | size_t name_len; 25 | unsigned char level_bitmap[32]; 26 | unsigned char level_bitmap_backup[32]; 27 | zc_arraylist_t *fit_rules; 28 | zc_arraylist_t *fit_rules_backup; 29 | } zlog_category_t; 30 | 31 | zlog_category_t *zlog_category_new(const char *name, zc_arraylist_t * rules); 32 | void zlog_category_del(zlog_category_t * a_category); 33 | void zlog_category_profile(zlog_category_t *a_category, int flag); 34 | 35 | int zlog_category_update_rules(zlog_category_t * a_category, zc_arraylist_t * new_rules); 36 | void zlog_category_commit_rules(zlog_category_t * a_category); 37 | void zlog_category_rollback_rules(zlog_category_t * a_category); 38 | 39 | int zlog_category_output(zlog_category_t * a_category, zlog_thread_t * a_thread); 40 | 41 | #define zlog_category_needless_level(a_category, lv) \ 42 | a_category && (zlog_env_conf->level > lv || !((a_category->level_bitmap[lv/8] >> (7 - lv % 8)) & 0x01)) 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /src/category_table.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "zc_defs.h" 21 | #include "category_table.h" 22 | 23 | void zlog_category_table_profile(zc_hashtable_t * categories, int flag) 24 | { 25 | zc_hashtable_entry_t *a_entry; 26 | zlog_category_t *a_category; 27 | 28 | zc_assert(categories,); 29 | zc_profile(flag, "-category_table[%p]-", categories); 30 | zc_hashtable_foreach(categories, a_entry) { 31 | a_category = (zlog_category_t *) a_entry->value; 32 | zlog_category_profile(a_category, flag); 33 | } 34 | return; 35 | } 36 | 37 | /*******************************************************************************/ 38 | 39 | void zlog_category_table_del(zc_hashtable_t * categories) 40 | { 41 | zc_assert(categories,); 42 | zc_hashtable_del(categories); 43 | zc_debug("zlog_category_table_del[%p]", categories); 44 | return; 45 | } 46 | 47 | zc_hashtable_t *zlog_category_table_new(void) 48 | { 49 | zc_hashtable_t *categories; 50 | 51 | categories = zc_hashtable_new(20, 52 | (zc_hashtable_hash_fn) zc_hashtable_str_hash, 53 | (zc_hashtable_equal_fn) zc_hashtable_str_equal, 54 | NULL, (zc_hashtable_del_fn) zlog_category_del); 55 | if (!categories) { 56 | zc_error("zc_hashtable_new fail"); 57 | return NULL; 58 | } else { 59 | zlog_category_table_profile(categories, ZC_DEBUG); 60 | return categories; 61 | } 62 | } 63 | /*******************************************************************************/ 64 | int zlog_category_table_update_rules(zc_hashtable_t * categories, zc_arraylist_t * new_rules) 65 | { 66 | zc_hashtable_entry_t *a_entry; 67 | zlog_category_t *a_category; 68 | 69 | zc_assert(categories, -1); 70 | zc_hashtable_foreach(categories, a_entry) { 71 | a_category = (zlog_category_t *) a_entry->value; 72 | if (zlog_category_update_rules(a_category, new_rules)) { 73 | zc_error("zlog_category_update_rules fail, try rollback"); 74 | return -1; 75 | } 76 | } 77 | return 0; 78 | } 79 | 80 | void zlog_category_table_commit_rules(zc_hashtable_t * categories) 81 | { 82 | zc_hashtable_entry_t *a_entry; 83 | zlog_category_t *a_category; 84 | 85 | zc_assert(categories,); 86 | zc_hashtable_foreach(categories, a_entry) { 87 | a_category = (zlog_category_t *) a_entry->value; 88 | zlog_category_commit_rules(a_category); 89 | } 90 | return; 91 | } 92 | 93 | void zlog_category_table_rollback_rules(zc_hashtable_t * categories) 94 | { 95 | zc_hashtable_entry_t *a_entry; 96 | zlog_category_t *a_category; 97 | 98 | zc_assert(categories,); 99 | zc_hashtable_foreach(categories, a_entry) { 100 | a_category = (zlog_category_t *) a_entry->value; 101 | zlog_category_rollback_rules(a_category); 102 | } 103 | return; 104 | } 105 | 106 | /*******************************************************************************/ 107 | zlog_category_t *zlog_category_table_fetch_category(zc_hashtable_t * categories, 108 | const char *category_name, zc_arraylist_t * rules) 109 | { 110 | zlog_category_t *a_category; 111 | 112 | zc_assert(categories, NULL); 113 | 114 | /* 1st find category in global category map */ 115 | a_category = zc_hashtable_get(categories, category_name); 116 | if (a_category) return a_category; 117 | 118 | /* else not found, create one */ 119 | a_category = zlog_category_new(category_name, rules); 120 | if (!a_category) { 121 | zc_error("zc_category_new fail"); 122 | return NULL; 123 | } 124 | 125 | if(zc_hashtable_put(categories, a_category->name, a_category)) { 126 | zc_error("zc_hashtable_put fail"); 127 | goto err; 128 | } 129 | 130 | return a_category; 131 | err: 132 | zlog_category_del(a_category); 133 | return NULL; 134 | } 135 | 136 | /*******************************************************************************/ 137 | -------------------------------------------------------------------------------- /src/category_table.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_category_table_h 17 | #define __zlog_category_table_h 18 | 19 | #include "zc_defs.h" 20 | #include "category.h" 21 | 22 | zc_hashtable_t *zlog_category_table_new(void); 23 | void zlog_category_table_del(zc_hashtable_t * categories); 24 | void zlog_category_table_profile(zc_hashtable_t * categories, int flag); 25 | 26 | /* if none, create new and return */ 27 | zlog_category_t *zlog_category_table_fetch_category( 28 | zc_hashtable_t * categories, 29 | const char *category_name, zc_arraylist_t * rules); 30 | 31 | int zlog_category_table_update_rules(zc_hashtable_t * categories, zc_arraylist_t * new_rules); 32 | void zlog_category_table_commit_rules(zc_hashtable_t * categories); 33 | void zlog_category_table_rollback_rules(zc_hashtable_t * categories); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /src/conf.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_conf_h 17 | #define __zlog_conf_h 18 | 19 | #include "zc_defs.h" 20 | #include "format.h" 21 | #include "rotater.h" 22 | 23 | typedef struct zlog_conf_s { 24 | char file[MAXLEN_PATH + 1]; 25 | char cfg_ptr[MAXLEN_CFG_LINE*MAXLINES_NO]; 26 | char mtime[20 + 1]; 27 | 28 | int strict_init; 29 | size_t buf_size_min; 30 | size_t buf_size_max; 31 | 32 | char rotate_lock_file[MAXLEN_CFG_LINE + 1]; 33 | zlog_rotater_t *rotater; 34 | 35 | char default_format_line[MAXLEN_CFG_LINE + 1]; 36 | zlog_format_t *default_format; 37 | 38 | unsigned int file_perms; 39 | size_t fsync_period; 40 | size_t reload_conf_period; 41 | 42 | zc_arraylist_t *levels; 43 | zc_arraylist_t *formats; 44 | zc_arraylist_t *rules; 45 | int time_cache_count; 46 | char log_level[MAXLEN_CFG_LINE + 1]; 47 | int level; 48 | } zlog_conf_t; 49 | 50 | extern zlog_conf_t * zlog_env_conf; 51 | 52 | zlog_conf_t *zlog_conf_new(const char *config); 53 | zlog_conf_t *zlog_conf_new_from_string(const char *config_string); 54 | void zlog_conf_del(zlog_conf_t * a_conf); 55 | void zlog_conf_profile(zlog_conf_t * a_conf, int flag); 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/event.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #define _GNU_SOURCE // For distros like Centos for syscall interface 17 | 18 | #include "fmacros.h" 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | #include "zc_defs.h" 34 | #include "event.h" 35 | #ifdef _WIN32 36 | #include 37 | #endif 38 | 39 | void zlog_event_profile(zlog_event_t * a_event, int flag) 40 | { 41 | zc_assert(a_event,); 42 | zc_profile(flag, "---event[%p][%s,%s][%s(%ld),%s(%ld),%ld,%d][%p,%s][%ld,%ld][%ld,%ld][%d]---", 43 | a_event, 44 | a_event->category_name, a_event->host_name, 45 | a_event->file, a_event->file_len, 46 | a_event->func, a_event->func_len, 47 | a_event->line, a_event->level, 48 | a_event->hex_buf, a_event->str_format, 49 | a_event->time_stamp.tv_sec, a_event->time_stamp.tv_usec, 50 | (long)a_event->pid, (long)a_event->tid, 51 | a_event->time_cache_count); 52 | return; 53 | } 54 | 55 | /*******************************************************************************/ 56 | 57 | void zlog_event_del(zlog_event_t * a_event) 58 | { 59 | zc_assert(a_event,); 60 | if (a_event->time_caches) free(a_event->time_caches); 61 | zc_debug("zlog_event_del[%p]", a_event); 62 | free(a_event); 63 | return; 64 | } 65 | 66 | zlog_event_t *zlog_event_new(int time_cache_count) 67 | { 68 | zlog_event_t *a_event; 69 | 70 | a_event = calloc(1, sizeof(zlog_event_t)); 71 | if (!a_event) { 72 | zc_error("calloc fail, errno[%d]", errno); 73 | return NULL; 74 | } 75 | 76 | a_event->time_caches = calloc(time_cache_count, sizeof(zlog_time_cache_t)); 77 | if (!a_event->time_caches) { 78 | zc_error("calloc fail, errno[%d]", errno); 79 | free(a_event); 80 | return NULL; 81 | } 82 | a_event->time_cache_count = time_cache_count; 83 | 84 | /* 85 | * at the zlog_init we gethostname, 86 | * u don't always change your hostname, eh? 87 | */ 88 | if (gethostname(a_event->host_name, sizeof(a_event->host_name) - 1)) { 89 | zc_error("gethostname fail, errno[%d]", errno); 90 | goto err; 91 | } 92 | 93 | a_event->host_name_len = strlen(a_event->host_name); 94 | 95 | /* tid is bound to a_event 96 | * as in whole lifecycle event persists 97 | * even fork to oth pid, tid not change 98 | */ 99 | a_event->tid = pthread_self(); 100 | 101 | a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)a_event->tid); 102 | a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "%x", (unsigned int)a_event->tid); 103 | 104 | #ifdef __linux__ 105 | a_event->ktid = syscall(SYS_gettid); 106 | #elif __APPLE__ 107 | uint64_t tid64; 108 | pthread_threadid_np(NULL, &tid64); 109 | a_event->tid = (pthread_t)tid64; 110 | #endif 111 | 112 | #if defined __linux__ || __APPLE__ 113 | a_event->ktid_str_len = sprintf(a_event->ktid_str, "%u", (unsigned int)a_event->ktid); 114 | #endif 115 | 116 | //zlog_event_profile(a_event, ZC_DEBUG); 117 | return a_event; 118 | err: 119 | zlog_event_del(a_event); 120 | return NULL; 121 | } 122 | 123 | /*******************************************************************************/ 124 | void zlog_event_set_fmt(zlog_event_t * a_event, 125 | char *category_name, size_t category_name_len, 126 | const char *file, size_t file_len, const char *func, size_t func_len, long line, int level, 127 | const char *str_format, va_list str_args) 128 | { 129 | /* 130 | * category_name point to zlog_category_output's category.name 131 | */ 132 | a_event->category_name = category_name; 133 | a_event->category_name_len = category_name_len; 134 | 135 | a_event->file = (char *) file; 136 | a_event->file_len = file_len; 137 | a_event->func = (char *) func; 138 | a_event->func_len = func_len; 139 | a_event->line = line; 140 | a_event->level = level; 141 | 142 | a_event->generate_cmd = ZLOG_FMT; 143 | a_event->str_format = str_format; 144 | va_copy(a_event->str_args, str_args); 145 | 146 | /* pid should fetch eveytime, as no one knows, 147 | * when does user fork his process 148 | * so clean here, and fetch at spec.c 149 | */ 150 | a_event->pid = (pid_t) 0; 151 | 152 | /* in a event's life cycle, time will be get when spec need, 153 | * and keep unchange though all event's life cycle 154 | * zlog_spec_write_time gettimeofday 155 | */ 156 | a_event->time_stamp.tv_sec = 0; 157 | return; 158 | } 159 | 160 | void zlog_event_set_hex(zlog_event_t * a_event, 161 | char *category_name, size_t category_name_len, 162 | const char *file, size_t file_len, const char *func, size_t func_len, long line, int level, 163 | const void *hex_buf, size_t hex_buf_len) 164 | { 165 | /* 166 | * category_name point to zlog_category_output's category.name 167 | */ 168 | a_event->category_name = category_name; 169 | a_event->category_name_len = category_name_len; 170 | 171 | a_event->file = (char *) file; 172 | a_event->file_len = file_len; 173 | a_event->func = (char *) func; 174 | a_event->func_len = func_len; 175 | a_event->line = line; 176 | a_event->level = level; 177 | 178 | a_event->generate_cmd = ZLOG_HEX; 179 | a_event->hex_buf = hex_buf; 180 | a_event->hex_buf_len = hex_buf_len; 181 | 182 | /* pid should fetch eveytime, as no one knows, 183 | * when does user fork his process 184 | * so clean here, and fetch at spec.c 185 | */ 186 | a_event->pid = (pid_t) 0; 187 | 188 | /* in a event's life cycle, time will be get when spec need, 189 | * and keep unchange though all event's life cycle 190 | */ 191 | a_event->time_stamp.tv_sec = 0; 192 | return; 193 | } 194 | -------------------------------------------------------------------------------- /src/event.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_event_h 17 | #define __zlog_event_h 18 | 19 | #include /* for pid_t */ 20 | #include /* for struct timeval */ 21 | #include /* for pthread_t */ 22 | #include /* for va_list */ 23 | #include "zc_defs.h" 24 | 25 | typedef enum { 26 | ZLOG_FMT = 0, 27 | ZLOG_HEX = 1, 28 | } zlog_event_cmd; 29 | 30 | typedef struct zlog_time_cache_s { 31 | char str[MAXLEN_CFG_LINE + 1]; 32 | size_t len; 33 | time_t sec; 34 | } zlog_time_cache_t; 35 | 36 | typedef struct { 37 | char *category_name; 38 | size_t category_name_len; 39 | char host_name[256 + 1]; 40 | size_t host_name_len; 41 | 42 | const char *file; 43 | size_t file_len; 44 | const char *func; 45 | size_t func_len; 46 | long line; 47 | int level; 48 | 49 | const void *hex_buf; 50 | size_t hex_buf_len; 51 | const char *str_format; 52 | va_list str_args; 53 | zlog_event_cmd generate_cmd; 54 | 55 | struct timeval time_stamp; 56 | 57 | time_t time_utc_sec; 58 | struct tm time_utc; 59 | time_t time_local_sec; 60 | struct tm time_local; 61 | 62 | zlog_time_cache_t *time_caches; 63 | int time_cache_count; 64 | 65 | pid_t pid; 66 | pid_t last_pid; 67 | char pid_str[30 + 1]; 68 | size_t pid_str_len; 69 | 70 | pthread_t tid; 71 | char tid_str[30 + 1]; 72 | size_t tid_str_len; 73 | 74 | char tid_hex_str[30 + 1]; 75 | size_t tid_hex_str_len; 76 | 77 | #if defined __linux__ || __APPLE__ 78 | pid_t ktid; 79 | char ktid_str[30+1]; 80 | size_t ktid_str_len; 81 | #endif 82 | } zlog_event_t; 83 | 84 | 85 | zlog_event_t *zlog_event_new(int time_cache_count); 86 | void zlog_event_del(zlog_event_t * a_event); 87 | void zlog_event_profile(zlog_event_t * a_event, int flag); 88 | 89 | void zlog_event_set_fmt(zlog_event_t * a_event, 90 | char *category_name, size_t category_name_len, 91 | const char *file, size_t file_len, const char *func, size_t func_len, long line, int level, 92 | const char *str_format, va_list str_args); 93 | 94 | void zlog_event_set_hex(zlog_event_t * a_event, 95 | char *category_name, size_t category_name_len, 96 | const char *file, size_t file_len, const char *func, size_t func_len, long line, int level, 97 | const void *hex_buf, size_t hex_buf_len); 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /src/fmacros.h: -------------------------------------------------------------------------------- 1 | #ifndef __zlog_fmacro_h 2 | #define __zlog_fmacro_h 3 | 4 | #ifndef _BSD_SOURCE 5 | #define _BSD_SOURCE 6 | #endif 7 | 8 | #ifndef _DEFAULT_SOURCE 9 | #define _DEFAULT_SOURCE 10 | #endif 11 | 12 | #if defined(__linux__) || defined(__OpenBSD__) || defined(_AIX) 13 | #ifndef _XOPEN_SOURCE 14 | #define _XOPEN_SOURCE 700 15 | #endif 16 | #ifndef _XOPEN_SOURCE_EXTENDED 17 | #define _XOPEN_SOURCE_EXTENDED 18 | #endif 19 | #if defined(__APPLE__) 20 | #include 21 | #else 22 | #ifndef _POSIX_C_SOURCE 23 | #define _POSIX_C_SOURCE 200809L 24 | #endif 25 | #endif 26 | #ifndef _XOPEN_SOURCE 27 | #define _XOPEN_SOURCE 28 | #endif 29 | #endif 30 | 31 | #ifndef _LARGEFILE_SOURCE 32 | #define _LARGEFILE_SOURCE 33 | #endif 34 | 35 | #ifndef _LARGEFILE_SOURCE 36 | #define _LARGEFILE_SOURCE 37 | #endif 38 | #ifndef _FILE_OFFSET_BITS 39 | #define _FILE_OFFSET_BITS 64 40 | #endif 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/format.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "zc_defs.h" 24 | #include "thread.h" 25 | #include "spec.h" 26 | #include "format.h" 27 | 28 | void zlog_format_profile(zlog_format_t * a_format, int flag) 29 | { 30 | 31 | zc_assert(a_format,); 32 | zc_profile(flag, "---format[%p][%s = %s(%p)]---", 33 | a_format, 34 | a_format->name, 35 | a_format->pattern, 36 | a_format->pattern_specs); 37 | 38 | #if 0 39 | int i; 40 | zlog_spec_t *a_spec; 41 | zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) { 42 | zlog_spec_profile(a_spec, flag); 43 | } 44 | #endif 45 | 46 | return; 47 | } 48 | 49 | /*******************************************************************************/ 50 | void zlog_format_del(zlog_format_t * a_format) 51 | { 52 | zc_assert(a_format,); 53 | if (a_format->pattern_specs) { 54 | zc_arraylist_del(a_format->pattern_specs); 55 | } 56 | zc_debug("zlog_format_del[%p]", a_format); 57 | free(a_format); 58 | return; 59 | } 60 | 61 | zlog_format_t *zlog_format_new(char *line, int * time_cache_count) 62 | { 63 | int nscan = 0; 64 | zlog_format_t *a_format = NULL; 65 | int nread = 0; 66 | const char *p_start; 67 | const char *p_end; 68 | char *p; 69 | char *q; 70 | zlog_spec_t *a_spec; 71 | 72 | zc_assert(line, NULL); 73 | 74 | a_format = calloc(1, sizeof(zlog_format_t)); 75 | if (!a_format) { 76 | zc_error("calloc fail, errno[%d]", errno); 77 | return NULL; 78 | } 79 | 80 | /* line default = "%d(%F %X.%l) %-6V (%c:%F:%L) - %m%n" 81 | * name default 82 | * pattern %d(%F %X.%l) %-6V (%c:%F:%L) - %m%n 83 | */ 84 | memset(a_format->name, 0x00, sizeof(a_format->name)); 85 | nread = 0; 86 | nscan = sscanf(line, " %[^= \t] = %n", a_format->name, &nread); 87 | if (nscan != 1) { 88 | zc_error("format[%s], syntax wrong", line); 89 | goto err; 90 | } 91 | 92 | if (*(line + nread) != '"') { 93 | zc_error("the 1st char of pattern is not \", line+nread[%s]", line+nread); 94 | goto err; 95 | } 96 | 97 | for (p = a_format->name; *p != '\0'; p++) { 98 | if ((!isalnum(*p)) && (*p != '_')) { 99 | zc_error("a_format->name[%s] character is not in [a-Z][0-9][_]", a_format->name); 100 | goto err; 101 | } 102 | } 103 | 104 | p_start = line + nread + 1; 105 | p_end = strrchr(p_start, '"'); 106 | if (!p_end) { 107 | zc_error("there is no \" at end of pattern, line[%s]", line); 108 | goto err; 109 | } 110 | 111 | if (p_end - p_start > sizeof(a_format->pattern) - 1) { 112 | zc_error("pattern is too long"); 113 | goto err; 114 | } 115 | memset(a_format->pattern, 0x00, sizeof(a_format->pattern)); 116 | memcpy(a_format->pattern, p_start, p_end - p_start); 117 | 118 | if (zc_str_replace_env(a_format->pattern, sizeof(a_format->pattern))) { 119 | zc_error("zc_str_replace_env fail"); 120 | goto err; 121 | } 122 | 123 | a_format->pattern_specs = 124 | zc_arraylist_new((zc_arraylist_del_fn) zlog_spec_del); 125 | if (!(a_format->pattern_specs)) { 126 | zc_error("zc_arraylist_new fail"); 127 | goto err; 128 | } 129 | 130 | for (p = a_format->pattern; *p != '\0'; p = q) { 131 | a_spec = zlog_spec_new(p, &q, time_cache_count); 132 | if (!a_spec) { 133 | zc_error("zlog_spec_new fail"); 134 | goto err; 135 | } 136 | 137 | if (zc_arraylist_add(a_format->pattern_specs, a_spec)) { 138 | zlog_spec_del(a_spec); 139 | zc_error("zc_arraylist_add fail"); 140 | goto err; 141 | } 142 | } 143 | 144 | zlog_format_profile(a_format, ZC_DEBUG); 145 | return a_format; 146 | err: 147 | zlog_format_del(a_format); 148 | return NULL; 149 | } 150 | 151 | /*******************************************************************************/ 152 | /* return 0 success, or buf is full 153 | * return -1 fail 154 | */ 155 | int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread) 156 | { 157 | int i; 158 | zlog_spec_t *a_spec; 159 | 160 | zlog_buf_restart(a_thread->msg_buf); 161 | 162 | zc_arraylist_foreach(a_format->pattern_specs, i, a_spec) { 163 | if (zlog_spec_gen_msg(a_spec, a_thread) == 0) { 164 | continue; 165 | } else { 166 | return -1; 167 | } 168 | } 169 | 170 | return 0; 171 | } 172 | -------------------------------------------------------------------------------- /src/format.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_format_h 17 | #define __zlog_format_h 18 | 19 | #include "thread.h" 20 | #include "zc_defs.h" 21 | 22 | typedef struct zlog_format_s zlog_format_t; 23 | 24 | struct zlog_format_s { 25 | char name[MAXLEN_CFG_LINE + 1]; 26 | char pattern[MAXLEN_CFG_LINE + 1]; 27 | zc_arraylist_t *pattern_specs; 28 | }; 29 | 30 | zlog_format_t *zlog_format_new(char *line, int * time_cache_count); 31 | void zlog_format_del(zlog_format_t * a_format); 32 | void zlog_format_profile(zlog_format_t * a_format, int flag); 33 | 34 | int zlog_format_gen_msg(zlog_format_t * a_format, zlog_thread_t * a_thread); 35 | 36 | #define zlog_format_has_name(a_format, fname) \ 37 | STRCMP(a_format->name, ==, fname) 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/level.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #ifndef _WIN32 20 | #include 21 | #else 22 | #include "zlog_win.h" 23 | #endif 24 | 25 | #include "zc_defs.h" 26 | #include "level.h" 27 | 28 | void zlog_level_profile(zlog_level_t *a_level, int flag) 29 | { 30 | zc_assert(a_level,); 31 | zc_profile(flag, "---level[%p][%d,%s,%s,%d,%d]---", 32 | a_level, 33 | a_level->int_level, 34 | a_level->str_uppercase, 35 | a_level->str_lowercase, 36 | (int) a_level->str_len, 37 | a_level->syslog_level); 38 | return; 39 | } 40 | 41 | /*******************************************************************************/ 42 | void zlog_level_del(zlog_level_t *a_level) 43 | { 44 | zc_assert(a_level,); 45 | zc_debug("zlog_level_del[%p]", a_level); 46 | free(a_level); 47 | return; 48 | } 49 | 50 | static int syslog_level_atoi(char *str) 51 | { 52 | /* guess no unix system will choose -187 53 | * as its syslog level, so it is a safe return value 54 | */ 55 | zc_assert(str, -187); 56 | 57 | if (STRICMP(str, ==, "LOG_EMERG")) 58 | return LOG_EMERG; 59 | if (STRICMP(str, ==, "LOG_ALERT")) 60 | return LOG_ALERT; 61 | if (STRICMP(str, ==, "LOG_CRIT")) 62 | return LOG_CRIT; 63 | if (STRICMP(str, ==, "LOG_ERR")) 64 | return LOG_ERR; 65 | if (STRICMP(str, ==, "LOG_WARNING")) 66 | return LOG_WARNING; 67 | if (STRICMP(str, ==, "LOG_NOTICE")) 68 | return LOG_NOTICE; 69 | if (STRICMP(str, ==, "LOG_INFO")) 70 | return LOG_INFO; 71 | if (STRICMP(str, ==, "LOG_DEBUG")) 72 | return LOG_DEBUG; 73 | 74 | zc_error("wrong syslog level[%s]", str); 75 | return -187; 76 | } 77 | 78 | /* line: TRACE = 10, LOG_ERR */ 79 | zlog_level_t *zlog_level_new(char *line) 80 | { 81 | zlog_level_t *a_level = NULL; 82 | int i; 83 | int nscan; 84 | char str[MAXLEN_CFG_LINE + 1]; 85 | int l = 0; 86 | char sl[MAXLEN_CFG_LINE + 1]; 87 | 88 | zc_assert(line, NULL); 89 | 90 | memset(str, 0x00, sizeof(str)); 91 | memset(sl, 0x00, sizeof(sl)); 92 | 93 | nscan = sscanf(line, " %[^= \t] = %d ,%s", str, &l, sl); 94 | if (nscan < 2) { 95 | zc_error("level[%s], syntax wrong", line); 96 | return NULL; 97 | } 98 | 99 | /* check level and str */ 100 | if ((l < 0) || (l > 255)) { 101 | zc_error("l[%d] not in [0,255], wrong", l); 102 | return NULL; 103 | } 104 | 105 | if (str[0] == '\0') { 106 | zc_error("str[0] = 0"); 107 | return NULL; 108 | } 109 | 110 | a_level = calloc(1, sizeof(zlog_level_t)); 111 | if (!a_level) { 112 | zc_error("calloc fail, errno[%d]", errno); 113 | return NULL; 114 | } 115 | 116 | a_level->int_level = l; 117 | 118 | /* fill syslog level */ 119 | if (sl[0] == '\0') { 120 | a_level->syslog_level = LOG_DEBUG; 121 | } else { 122 | a_level->syslog_level = syslog_level_atoi(sl); 123 | if (a_level->syslog_level == -187) { 124 | zc_error("syslog_level_atoi fail"); 125 | goto err; 126 | } 127 | } 128 | 129 | /* strncpy and toupper(str) */ 130 | for (i = 0; (i < sizeof(a_level->str_uppercase) - 1) && str[i] != '\0'; i++) { 131 | (a_level->str_uppercase)[i] = toupper(str[i]); 132 | (a_level->str_lowercase)[i] = tolower(str[i]); 133 | } 134 | 135 | if (str[i] != '\0') { 136 | /* overflow */ 137 | zc_error("not enough space for str, str[%s] > %d", str, i); 138 | goto err; 139 | } else { 140 | (a_level->str_uppercase)[i] = '\0'; 141 | (a_level->str_lowercase)[i] = '\0'; 142 | } 143 | 144 | a_level->str_len = i; 145 | 146 | //zlog_level_profile(a_level, ZC_DEBUG); 147 | return a_level; 148 | err: 149 | zc_error("line[%s]", line); 150 | zlog_level_del(a_level); 151 | return NULL; 152 | } 153 | -------------------------------------------------------------------------------- /src/level.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_level_h 17 | #define __zlog_level_h 18 | 19 | #include "zc_defs.h" 20 | 21 | typedef struct zlog_level_s { 22 | int int_level; 23 | char str_uppercase[MAXLEN_PATH + 1]; 24 | char str_lowercase[MAXLEN_PATH + 1]; 25 | size_t str_len; 26 | int syslog_level; 27 | } zlog_level_t; 28 | 29 | zlog_level_t *zlog_level_new(char *line); 30 | void zlog_level_del(zlog_level_t *a_level); 31 | void zlog_level_profile(zlog_level_t *a_level, int flag); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/level_list.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #ifdef _WIN32 20 | #include "zlog_win.h" 21 | #endif 22 | 23 | #include "zc_defs.h" 24 | #include "level.h" 25 | #include "level_list.h" 26 | 27 | /* zlog_level_list == zc_arraylist_t */ 28 | 29 | void zlog_level_list_profile(zc_arraylist_t *levels, int flag) 30 | { 31 | int i; 32 | zlog_level_t *a_level; 33 | 34 | zc_assert(levels,); 35 | zc_profile(flag, "--level_list[%p]--", levels); 36 | zc_arraylist_foreach(levels, i, a_level) { 37 | /* skip empty slots */ 38 | if (a_level) zlog_level_profile(a_level, flag); 39 | } 40 | return; 41 | } 42 | 43 | /*******************************************************************************/ 44 | void zlog_level_list_del(zc_arraylist_t *levels) 45 | { 46 | zc_assert(levels,); 47 | zc_arraylist_del(levels); 48 | zc_debug("zc_level_list_del[%p]", levels); 49 | return; 50 | } 51 | 52 | static int zlog_level_list_set_default(zc_arraylist_t *levels) 53 | { 54 | return zlog_level_list_set(levels, "* = 0, LOG_INFO") 55 | || zlog_level_list_set(levels, "DEBUG = 20, LOG_DEBUG") 56 | || zlog_level_list_set(levels, "INFO = 40, LOG_INFO") 57 | || zlog_level_list_set(levels, "NOTICE = 60, LOG_NOTICE") 58 | || zlog_level_list_set(levels, "WARN = 80, LOG_WARNING") 59 | || zlog_level_list_set(levels, "ERROR = 100, LOG_ERR") 60 | || zlog_level_list_set(levels, "FATAL = 120, LOG_ALERT") 61 | || zlog_level_list_set(levels, "UNKNOWN = 254, LOG_ERR") 62 | || zlog_level_list_set(levels, "! = 255, LOG_INFO"); 63 | } 64 | 65 | zc_arraylist_t *zlog_level_list_new(void) 66 | { 67 | zc_arraylist_t *levels; 68 | 69 | levels = zc_arraylist_new((zc_arraylist_del_fn)zlog_level_del); 70 | if (!levels) { 71 | zc_error("zc_arraylist_new fail"); 72 | return NULL; 73 | } 74 | 75 | if (zlog_level_list_set_default(levels)) { 76 | zc_error("zlog_level_set_default fail"); 77 | goto err; 78 | } 79 | 80 | //zlog_level_list_profile(levels, ZC_DEBUG); 81 | return levels; 82 | err: 83 | zc_arraylist_del(levels); 84 | return NULL; 85 | } 86 | 87 | /*******************************************************************************/ 88 | int zlog_level_list_set(zc_arraylist_t *levels, char *line) 89 | { 90 | zlog_level_t *a_level; 91 | 92 | a_level = zlog_level_new(line); 93 | if (!a_level) { 94 | zc_error("zlog_level_new fail"); 95 | return -1; 96 | } 97 | 98 | if (zc_arraylist_set(levels, a_level->int_level, a_level)) { 99 | zc_error("zc_arraylist_set fail"); 100 | goto err; 101 | } 102 | 103 | return 0; 104 | err: 105 | zc_error("line[%s]", line); 106 | zlog_level_del(a_level); 107 | return -1; 108 | } 109 | 110 | zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l) 111 | { 112 | zlog_level_t *a_level; 113 | 114 | #if 0 115 | if ((l <= 0) || (l > 254)) { 116 | /* illegal input from zlog() */ 117 | zc_error("l[%d] not in (0,254), set to UNKOWN", l); 118 | l = 254; 119 | } 120 | #endif 121 | 122 | a_level = zc_arraylist_get(levels, l); 123 | if (a_level) { 124 | return a_level; 125 | } else { 126 | /* empty slot */ 127 | zc_error("l[%d] not in (0,254), or has no level defined," 128 | "see configure file define, set to UNKOWN", l); 129 | return zc_arraylist_get(levels, 254); 130 | } 131 | } 132 | 133 | /*******************************************************************************/ 134 | 135 | int zlog_level_list_atoi(zc_arraylist_t *levels, char *str) 136 | { 137 | int i; 138 | zlog_level_t *a_level; 139 | 140 | if (str == NULL || *str == '\0') { 141 | zc_error("str is [%s], can't find level", str); 142 | return -1; 143 | } 144 | 145 | zc_arraylist_foreach(levels, i, a_level) { 146 | if (a_level && STRICMP(str, ==, a_level->str_uppercase)) { 147 | return i; 148 | } 149 | } 150 | 151 | zc_error("str[%s] can't found in level list", str); 152 | return -1; 153 | } 154 | 155 | -------------------------------------------------------------------------------- /src/level_list.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_level_list_h 17 | #define __zlog_level_list_h 18 | 19 | #include "zc_defs.h" 20 | #include "level.h" 21 | 22 | zc_arraylist_t *zlog_level_list_new(void); 23 | void zlog_level_list_del(zc_arraylist_t *levels); 24 | void zlog_level_list_profile(zc_arraylist_t *levels, int flag); 25 | 26 | /* conf init use, slow */ 27 | /* if l is wrong or str=="", return -1 */ 28 | int zlog_level_list_set(zc_arraylist_t *levels, char *line); 29 | 30 | /* spec output use, fast */ 31 | /* rule output use, fast */ 32 | /* if not found, return levels[254] */ 33 | zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l); 34 | 35 | /* rule init use, slow */ 36 | /* if not found, return -1 */ 37 | int zlog_level_list_atoi(zc_arraylist_t *levels, char *str); 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/lockfile.c: -------------------------------------------------------------------------------- 1 | /** 2 | * ============================================================================= 3 | * 4 | * \file lockfile.c 5 | * \breif 6 | * \version 1.0 7 | * \date 2013-07-07 11:55:38 8 | * \author Song min.Li (Li), lisongmin@126.com 9 | * \copyright Copyright (c) 2013, skybility 10 | * 11 | * ============================================================================= 12 | */ 13 | 14 | #include "lockfile.h" 15 | #include "zc_profile.h" 16 | 17 | LOCK_FD lock_file(char* path) { 18 | if (!path || strlen(path) <= 0) { 19 | return INVALID_LOCK_FD; 20 | } 21 | #ifdef _WIN32 22 | LOCK_FD fd = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 23 | if (fd == INVALID_LOCK_FD) { 24 | DWORD err = GetLastError(); 25 | zc_error("lock file error : %d ", err); 26 | } 27 | #else 28 | LOCK_FD fd = open(path, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); 29 | if (fd == INVALID_LOCK_FD) { 30 | zc_error("lock file error : %s ", strerror(errno)); 31 | } 32 | #endif 33 | return fd; 34 | } 35 | 36 | bool unlock_file(LOCK_FD fd) { 37 | if (fd == INVALID_LOCK_FD) { 38 | return true; 39 | } 40 | #ifdef _WIN32 41 | bool ret = CloseHandle(fd); 42 | if (ret == false) { 43 | DWORD err = GetLastError(); 44 | zc_error("unlock file error : %d ", err); 45 | } 46 | #else 47 | bool ret = close(fd) == 0; 48 | if (ret == false) { 49 | zc_error("unlock file error : %s ", strerror(errno)); 50 | } 51 | #endif 52 | return ret; 53 | } 54 | -------------------------------------------------------------------------------- /src/lockfile.h: -------------------------------------------------------------------------------- 1 | /** 2 | * ============================================================================= 3 | * 4 | * \file lockfile.h 5 | * \breif 6 | * \version 1.0 7 | * \date 2013-07-07 12:34:20 8 | * \author Song min.Li (Li), lisongmin@126.com 9 | * \copyright Copyright (c) 2013, skybility 10 | * 11 | * ============================================================================= 12 | */ 13 | 14 | #ifndef __ZLOG_LOCK_FILE_H__ 15 | #define __ZLOG_LOCK_FILE_H__ 16 | 17 | #ifdef _WIN32 18 | #include 19 | #include 20 | #define LOCK_FD HANDLE 21 | #define INVALID_LOCK_FD INVALID_HANDLE_VALUE 22 | #else //_WIN32 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #define LOCK_FD int 30 | #define INVALID_LOCK_FD -1 31 | #endif 32 | #include 33 | 34 | /** 35 | * lock file. 36 | */ 37 | LOCK_FD lock_file(char* path); 38 | 39 | /** 40 | * unlock file. 41 | */ 42 | bool unlock_file(LOCK_FD fd); 43 | 44 | #endif //__ZLOG_LOCK_FILE_H__ 45 | -------------------------------------------------------------------------------- /src/mdc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "mdc.h" 21 | #include "zc_defs.h" 22 | 23 | void zlog_mdc_profile(zlog_mdc_t *a_mdc, int flag) 24 | { 25 | zc_hashtable_entry_t *a_entry; 26 | zlog_mdc_kv_t *a_mdc_kv; 27 | 28 | zc_assert(a_mdc,); 29 | zc_profile(flag, "---mdc[%p]---", a_mdc); 30 | 31 | zc_hashtable_foreach(a_mdc->tab, a_entry) { 32 | a_mdc_kv = a_entry->value; 33 | zc_profile(flag, "----mdc_kv[%p][%s]-[%s]----", 34 | a_mdc_kv, 35 | a_mdc_kv->key, a_mdc_kv->value); 36 | } 37 | return; 38 | } 39 | /*******************************************************************************/ 40 | void zlog_mdc_del(zlog_mdc_t * a_mdc) 41 | { 42 | zc_assert(a_mdc,); 43 | if (a_mdc->tab) zc_hashtable_del(a_mdc->tab); 44 | zc_debug("zlog_mdc_del[%p]", a_mdc); 45 | free(a_mdc); 46 | return; 47 | } 48 | 49 | static void zlog_mdc_kv_del(zlog_mdc_kv_t * a_mdc_kv) 50 | { 51 | zc_debug("zlog_mdc_kv_del[%p]", a_mdc_kv); 52 | free(a_mdc_kv); 53 | } 54 | 55 | static zlog_mdc_kv_t *zlog_mdc_kv_new(const char *key, const char *value) 56 | { 57 | zlog_mdc_kv_t *a_mdc_kv; 58 | 59 | a_mdc_kv = calloc(1, sizeof(zlog_mdc_kv_t)); 60 | if (!a_mdc_kv) { 61 | zc_error("calloc fail, errno[%d]", errno); 62 | return NULL; 63 | } 64 | 65 | snprintf(a_mdc_kv->key, sizeof(a_mdc_kv->key), "%s", key); 66 | a_mdc_kv->value_len = snprintf(a_mdc_kv->value, sizeof(a_mdc_kv->value), "%s", value); 67 | return a_mdc_kv; 68 | } 69 | 70 | zlog_mdc_t *zlog_mdc_new(void) 71 | { 72 | zlog_mdc_t *a_mdc; 73 | 74 | a_mdc = calloc(1, sizeof(zlog_mdc_t)); 75 | if (!a_mdc) { 76 | zc_error("calloc fail, errno[%d]", errno); 77 | return NULL; 78 | } 79 | 80 | a_mdc->tab = zc_hashtable_new(20, 81 | zc_hashtable_str_hash, 82 | zc_hashtable_str_equal, NULL, 83 | (zc_hashtable_del_fn) zlog_mdc_kv_del); 84 | if (!a_mdc->tab) { 85 | zc_error("zc_hashtable_new fail"); 86 | goto err; 87 | } 88 | 89 | //zlog_mdc_profile(a_mdc, ZC_DEBUG); 90 | return a_mdc; 91 | err: 92 | zlog_mdc_del(a_mdc); 93 | return NULL; 94 | } 95 | 96 | /*******************************************************************************/ 97 | int zlog_mdc_put(zlog_mdc_t * a_mdc, const char *key, const char *value) 98 | { 99 | zlog_mdc_kv_t *a_mdc_kv; 100 | 101 | a_mdc_kv = zlog_mdc_kv_new(key, value); 102 | if (!a_mdc_kv) { 103 | zc_error("zlog_mdc_kv_new failed"); 104 | return -1; 105 | } 106 | 107 | if (zc_hashtable_put(a_mdc->tab, a_mdc_kv->key, a_mdc_kv)) { 108 | zc_error("zc_hashtable_put fail"); 109 | zlog_mdc_kv_del(a_mdc_kv); 110 | return -1; 111 | } 112 | 113 | return 0; 114 | } 115 | 116 | void zlog_mdc_clean(zlog_mdc_t * a_mdc) 117 | { 118 | zc_hashtable_clean(a_mdc->tab); 119 | return; 120 | } 121 | 122 | char *zlog_mdc_get(zlog_mdc_t * a_mdc, const char *key) 123 | { 124 | zlog_mdc_kv_t *a_mdc_kv; 125 | 126 | a_mdc_kv = zc_hashtable_get(a_mdc->tab, key); 127 | if (!a_mdc_kv) { 128 | zc_error("zc_hashtable_get fail"); 129 | return NULL; 130 | } else { 131 | return a_mdc_kv->value; 132 | } 133 | } 134 | 135 | zlog_mdc_kv_t *zlog_mdc_get_kv(zlog_mdc_t * a_mdc, const char *key) 136 | { 137 | zlog_mdc_kv_t *a_mdc_kv; 138 | 139 | a_mdc_kv = zc_hashtable_get(a_mdc->tab, key); 140 | if (!a_mdc_kv) { 141 | zc_error("zc_hashtable_get fail"); 142 | return NULL; 143 | } else { 144 | return a_mdc_kv; 145 | } 146 | } 147 | 148 | void zlog_mdc_remove(zlog_mdc_t * a_mdc, const char *key) 149 | { 150 | zc_hashtable_remove(a_mdc->tab, key); 151 | return; 152 | } 153 | -------------------------------------------------------------------------------- /src/mdc.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_mdc_h 17 | #define __zlog_mdc_h 18 | 19 | #include "zc_defs.h" 20 | 21 | typedef struct zlog_mdc_s zlog_mdc_t; 22 | struct zlog_mdc_s { 23 | zc_hashtable_t *tab; 24 | }; 25 | 26 | zlog_mdc_t *zlog_mdc_new(void); 27 | void zlog_mdc_del(zlog_mdc_t * a_mdc); 28 | void zlog_mdc_profile(zlog_mdc_t *a_mdc, int flag); 29 | 30 | void zlog_mdc_clean(zlog_mdc_t * a_mdc); 31 | int zlog_mdc_put(zlog_mdc_t * a_mdc, const char *key, const char *value); 32 | char *zlog_mdc_get(zlog_mdc_t * a_mdc, const char *key); 33 | void zlog_mdc_remove(zlog_mdc_t * a_mdc, const char *key); 34 | 35 | typedef struct zlog_mdc_kv_s { 36 | char key[MAXLEN_PATH + 1]; 37 | char value[MAXLEN_PATH + 1]; 38 | size_t value_len; 39 | } zlog_mdc_kv_t; 40 | 41 | zlog_mdc_kv_t *zlog_mdc_get_kv(zlog_mdc_t * a_mdc, const char *key); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /src/record.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "errno.h" 16 | #include "zc_defs.h" 17 | #include "record.h" 18 | 19 | void zlog_record_profile(zlog_record_t *a_record, int flag) 20 | { 21 | zc_assert(a_record,); 22 | zc_profile(flag, "--record:[%p][%s:%p]--", a_record, a_record->name, a_record->output); 23 | return; 24 | } 25 | 26 | void zlog_record_del(zlog_record_t *a_record) 27 | { 28 | zc_assert(a_record,); 29 | zc_debug("zlog_record_del[%p]", a_record); 30 | free(a_record); 31 | return; 32 | } 33 | 34 | zlog_record_t *zlog_record_new(const char *name, zlog_record_fn output) 35 | { 36 | zlog_record_t *a_record; 37 | 38 | zc_assert(name, NULL); 39 | zc_assert(output, NULL); 40 | 41 | a_record = calloc(1, sizeof(zlog_record_t)); 42 | if (!a_record) { 43 | zc_error("calloc fail, errno[%d]", errno); 44 | return NULL; 45 | } 46 | 47 | if (strlen(name) > sizeof(a_record->name) - 1) { 48 | zc_error("name[%s] is too long", name); 49 | goto err; 50 | } 51 | 52 | strcpy(a_record->name, name); 53 | a_record->output = output; 54 | 55 | zlog_record_profile(a_record, ZC_DEBUG); 56 | return a_record; 57 | err: 58 | zlog_record_del(a_record); 59 | return NULL; 60 | } 61 | -------------------------------------------------------------------------------- /src/record.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_record_h 17 | #define __zlog_record_h 18 | 19 | #include "zc_defs.h" 20 | 21 | /* record is user-defined output function and it's name from configure file */ 22 | typedef struct zlog_msg_s { 23 | char *buf; 24 | size_t len; 25 | char *path; 26 | } zlog_msg_t; /* 3 of this first, see need thread or not later */ 27 | 28 | typedef int (*zlog_record_fn)(zlog_msg_t * msg); 29 | 30 | typedef struct zlog_record_s { 31 | char name[MAXLEN_PATH + 1]; 32 | zlog_record_fn output; 33 | } zlog_record_t; 34 | 35 | zlog_record_t *zlog_record_new(const char *name, zlog_record_fn output); 36 | void zlog_record_del(zlog_record_t *a_record); 37 | void zlog_record_profile(zlog_record_t *a_record, int flag); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/record_table.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "zc_defs.h" 21 | #include "record_table.h" 22 | 23 | void zlog_record_table_profile(zc_hashtable_t * records, int flag) 24 | { 25 | zc_hashtable_entry_t *a_entry; 26 | zlog_record_t *a_record; 27 | 28 | zc_assert(records,); 29 | zc_profile(flag, "-record_table[%p]-", records); 30 | zc_hashtable_foreach(records, a_entry) { 31 | a_record = (zlog_record_t *) a_entry->value; 32 | zlog_record_profile(a_record, flag); 33 | } 34 | return; 35 | } 36 | 37 | /*******************************************************************************/ 38 | 39 | void zlog_record_table_del(zc_hashtable_t * records) 40 | { 41 | zc_assert(records,); 42 | zc_hashtable_del(records); 43 | zc_debug("zlog_record_table_del[%p]", records); 44 | return; 45 | } 46 | 47 | zc_hashtable_t *zlog_record_table_new(void) 48 | { 49 | zc_hashtable_t *records; 50 | 51 | records = zc_hashtable_new(20, 52 | (zc_hashtable_hash_fn) zc_hashtable_str_hash, 53 | (zc_hashtable_equal_fn) zc_hashtable_str_equal, 54 | NULL, (zc_hashtable_del_fn) zlog_record_del); 55 | if (!records) { 56 | zc_error("zc_hashtable_new fail"); 57 | return NULL; 58 | } else { 59 | zlog_record_table_profile(records, ZC_DEBUG); 60 | return records; 61 | } 62 | } 63 | /*******************************************************************************/ 64 | -------------------------------------------------------------------------------- /src/record_table.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_record_table_h 17 | #define __zlog_record_table_h 18 | 19 | #include "zc_defs.h" 20 | #include "record.h" 21 | 22 | zc_hashtable_t *zlog_record_table_new(void); 23 | void zlog_record_table_del(zc_hashtable_t * records); 24 | void zlog_record_table_profile(zc_hashtable_t * records, int flag); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/rotater.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_rotater_h 17 | #define __zlog_rotater_h 18 | 19 | #include "zc_defs.h" 20 | #include "lockfile.h" 21 | 22 | typedef struct zlog_rotater_s { 23 | pthread_mutex_t lock_mutex; 24 | char *lock_file; 25 | LOCK_FD lock_fd; 26 | 27 | /* single-use members */ 28 | char *base_path; /* aa.log */ 29 | char *archive_path; /* aa.#5i.log */ 30 | char glob_path[MAXLEN_PATH + 1]; /* aa.*.log */ 31 | size_t num_start_len; /* 3, offset to glob_path */ 32 | size_t num_end_len; /* 6, offset to glob_path */ 33 | int num_width; /* 5 */ 34 | int mv_type; /* ROLLING or SEQUENCE */ 35 | int max_count; 36 | zc_arraylist_t *files; 37 | } zlog_rotater_t; 38 | 39 | zlog_rotater_t *zlog_rotater_new(char *lock_file); 40 | void zlog_rotater_del(zlog_rotater_t *a_rotater); 41 | 42 | /* 43 | * return 44 | * -1 fail 45 | * 0 no rotate, or rotate and success 46 | */ 47 | int zlog_rotater_rotate(zlog_rotater_t *a_rotater, 48 | char *base_path, size_t msg_len, 49 | char *archive_path, long archive_max_size, int archive_max_count); 50 | 51 | void zlog_rotater_profile(zlog_rotater_t *a_rotater, int flag); 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/rule.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | /** 17 | * @file rule.h 18 | * @brief rule decide to output in format by category & level 19 | */ 20 | 21 | #ifndef __zlog_rule_h 22 | #define __zlog_rule_h 23 | 24 | #include 25 | #include 26 | 27 | #include "zc_defs.h" 28 | #include "format.h" 29 | #include "thread.h" 30 | #include "rotater.h" 31 | #include "record.h" 32 | 33 | typedef struct zlog_rule_s zlog_rule_t; 34 | 35 | typedef int (*zlog_rule_output_fn) (zlog_rule_t * a_rule, zlog_thread_t * a_thread); 36 | 37 | struct zlog_rule_s { 38 | char category[MAXLEN_CFG_LINE + 1]; 39 | char compare_char; 40 | /* 41 | * [*] log all level 42 | * [.] log level >= rule level, default 43 | * [=] log level == rule level 44 | * [!] log level != rule level 45 | */ 46 | int level; 47 | unsigned char level_bitmap[32]; /* for category determine whether output or not */ 48 | 49 | unsigned int file_perms; 50 | int file_open_flags; 51 | 52 | char file_path[MAXLEN_PATH + 1]; 53 | zc_arraylist_t *dynamic_specs; 54 | int static_fd; 55 | dev_t static_dev; 56 | ino_t static_ino; 57 | 58 | long archive_max_size; 59 | int archive_max_count; 60 | char archive_path[MAXLEN_PATH + 1]; 61 | zc_arraylist_t *archive_specs; 62 | 63 | FILE *pipe_fp; 64 | int pipe_fd; 65 | 66 | size_t fsync_period; 67 | size_t fsync_count; 68 | 69 | zc_arraylist_t *levels; 70 | int syslog_facility; 71 | 72 | zlog_format_t *format; 73 | zlog_rule_output_fn output; 74 | 75 | char record_name[MAXLEN_PATH + 1]; 76 | char record_path[MAXLEN_PATH + 1]; 77 | zlog_record_fn record_func; 78 | }; 79 | 80 | zlog_rule_t *zlog_rule_new(char * line, 81 | zc_arraylist_t * levels, 82 | zlog_format_t * default_format, 83 | zc_arraylist_t * formats, 84 | unsigned int file_perms, 85 | size_t fsync_period, 86 | int * time_cache_count); 87 | 88 | void zlog_rule_del(zlog_rule_t * a_rule); 89 | void zlog_rule_profile(zlog_rule_t * a_rule, int flag); 90 | int zlog_rule_match_category(zlog_rule_t * a_rule, char *category); 91 | int zlog_rule_is_wastebin(zlog_rule_t * a_rule); 92 | int zlog_rule_set_record(zlog_rule_t * a_rule, zc_hashtable_t *records); 93 | int zlog_rule_output(zlog_rule_t * a_rule, zlog_thread_t * a_thread); 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /src/spec.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_spec_h 17 | #define __zlog_spec_h 18 | 19 | #include "event.h" 20 | #include "buf.h" 21 | #include "thread.h" 22 | 23 | typedef struct zlog_spec_s zlog_spec_t; 24 | 25 | /* write buf, according to each spec's Conversion Characters */ 26 | typedef int (*zlog_spec_write_fn) (zlog_spec_t * a_spec, 27 | zlog_thread_t * a_thread, 28 | zlog_buf_t * a_buf); 29 | 30 | /* gen a_thread->msg or gen a_thread->path by using write_fn */ 31 | typedef int (*zlog_spec_gen_fn) (zlog_spec_t * a_spec, 32 | zlog_thread_t * a_thread); 33 | 34 | struct zlog_spec_s { 35 | char *str; 36 | int len; 37 | 38 | char time_fmt[MAXLEN_CFG_LINE + 1]; 39 | int time_cache_index; 40 | char mdc_key[MAXLEN_PATH + 1]; 41 | 42 | char print_fmt[MAXLEN_CFG_LINE + 1]; 43 | int left_adjust; 44 | int left_fill_zeros; 45 | size_t max_width; 46 | size_t min_width; 47 | 48 | zlog_spec_write_fn write_buf; 49 | zlog_spec_gen_fn gen_msg; 50 | zlog_spec_gen_fn gen_path; 51 | zlog_spec_gen_fn gen_archive_path; 52 | }; 53 | 54 | zlog_spec_t *zlog_spec_new(char *pattern_start, char **pattern_end, int * time_cache_count); 55 | void zlog_spec_del(zlog_spec_t * a_spec); 56 | void zlog_spec_profile(zlog_spec_t * a_spec, int flag); 57 | 58 | #define zlog_spec_gen_msg(a_spec, a_thread) \ 59 | a_spec->gen_msg(a_spec, a_thread) 60 | 61 | #define zlog_spec_gen_path(a_spec, a_thread) \ 62 | a_spec->gen_path(a_spec, a_thread) 63 | 64 | #define zlog_spec_gen_archive_path(a_spec, a_thread) \ 65 | a_spec->gen_archive_path(a_spec, a_thread) 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /src/thread.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | 19 | #include "zc_defs.h" 20 | #include "event.h" 21 | #include "buf.h" 22 | #include "thread.h" 23 | #include "mdc.h" 24 | 25 | void zlog_thread_profile(zlog_thread_t * a_thread, int flag) 26 | { 27 | zc_assert(a_thread,); 28 | zc_profile(flag, "--thread[%p][%p][%p][%p,%p,%p,%p,%p]--", 29 | a_thread, 30 | a_thread->mdc, 31 | a_thread->event, 32 | a_thread->pre_path_buf, 33 | a_thread->path_buf, 34 | a_thread->archive_path_buf, 35 | a_thread->pre_msg_buf, 36 | a_thread->msg_buf); 37 | 38 | zlog_mdc_profile(a_thread->mdc, flag); 39 | zlog_event_profile(a_thread->event, flag); 40 | zlog_buf_profile(a_thread->pre_path_buf, flag); 41 | zlog_buf_profile(a_thread->path_buf, flag); 42 | zlog_buf_profile(a_thread->archive_path_buf, flag); 43 | zlog_buf_profile(a_thread->pre_msg_buf, flag); 44 | zlog_buf_profile(a_thread->msg_buf, flag); 45 | return; 46 | } 47 | /*******************************************************************************/ 48 | void zlog_thread_del(zlog_thread_t * a_thread) 49 | { 50 | zc_assert(a_thread,); 51 | if (a_thread->mdc) 52 | zlog_mdc_del(a_thread->mdc); 53 | if (a_thread->event) 54 | zlog_event_del(a_thread->event); 55 | if (a_thread->pre_path_buf) 56 | zlog_buf_del(a_thread->pre_path_buf); 57 | if (a_thread->path_buf) 58 | zlog_buf_del(a_thread->path_buf); 59 | if (a_thread->archive_path_buf) 60 | zlog_buf_del(a_thread->archive_path_buf); 61 | if (a_thread->pre_msg_buf) 62 | zlog_buf_del(a_thread->pre_msg_buf); 63 | if (a_thread->msg_buf) 64 | zlog_buf_del(a_thread->msg_buf); 65 | 66 | zc_debug("zlog_thread_del[%p]", a_thread); 67 | free(a_thread); 68 | return; 69 | } 70 | 71 | zlog_thread_t *zlog_thread_new(int init_version, size_t buf_size_min, size_t buf_size_max, int time_cache_count) 72 | { 73 | zlog_thread_t *a_thread; 74 | 75 | a_thread = calloc(1, sizeof(zlog_thread_t)); 76 | if (!a_thread) { 77 | zc_error("calloc fail, errno[%d]", errno); 78 | return NULL; 79 | } 80 | 81 | a_thread->init_version = init_version; 82 | 83 | a_thread->mdc = zlog_mdc_new(); 84 | if (!a_thread->mdc) { 85 | zc_error("zlog_mdc_new fail"); 86 | goto err; 87 | } 88 | 89 | a_thread->event = zlog_event_new(time_cache_count); 90 | if (!a_thread->event) { 91 | zc_error("zlog_event_new fail"); 92 | goto err; 93 | } 94 | 95 | a_thread->pre_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL); 96 | if (!a_thread->pre_path_buf) { 97 | zc_error("zlog_buf_new fail"); 98 | goto err; 99 | } 100 | 101 | a_thread->path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL); 102 | if (!a_thread->path_buf) { 103 | zc_error("zlog_buf_new fail"); 104 | goto err; 105 | } 106 | 107 | a_thread->archive_path_buf = zlog_buf_new(MAXLEN_PATH + 1, MAXLEN_PATH + 1, NULL); 108 | if (!a_thread->archive_path_buf) { 109 | zc_error("zlog_buf_new fail"); 110 | goto err; 111 | } 112 | 113 | a_thread->pre_msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE); 114 | if (!a_thread->pre_msg_buf) { 115 | zc_error("zlog_buf_new fail"); 116 | goto err; 117 | } 118 | 119 | a_thread->msg_buf = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE); 120 | if (!a_thread->msg_buf) { 121 | zc_error("zlog_buf_new fail"); 122 | goto err; 123 | } 124 | 125 | 126 | //zlog_thread_profile(a_thread, ZC_DEBUG); 127 | return a_thread; 128 | err: 129 | zlog_thread_del(a_thread); 130 | return NULL; 131 | } 132 | 133 | /*******************************************************************************/ 134 | int zlog_thread_rebuild_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max) 135 | { 136 | zlog_buf_t *pre_msg_buf_new = NULL; 137 | zlog_buf_t *msg_buf_new = NULL; 138 | zc_assert(a_thread, -1); 139 | 140 | if ( (a_thread->msg_buf->size_min == buf_size_min) 141 | && (a_thread->msg_buf->size_max == buf_size_max)) { 142 | zc_debug("buf size not changed, no need rebuild"); 143 | return 0; 144 | } 145 | 146 | pre_msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE); 147 | if (!pre_msg_buf_new) { 148 | zc_error("zlog_buf_new fail"); 149 | goto err; 150 | } 151 | 152 | msg_buf_new = zlog_buf_new(buf_size_min, buf_size_max, "..." FILE_NEWLINE); 153 | if (!msg_buf_new) { 154 | zc_error("zlog_buf_new fail"); 155 | goto err; 156 | } 157 | 158 | zlog_buf_del(a_thread->pre_msg_buf); 159 | a_thread->pre_msg_buf = pre_msg_buf_new; 160 | 161 | zlog_buf_del(a_thread->msg_buf); 162 | a_thread->msg_buf = msg_buf_new; 163 | 164 | return 0; 165 | err: 166 | if (pre_msg_buf_new) zlog_buf_del(pre_msg_buf_new); 167 | if (msg_buf_new) zlog_buf_del(msg_buf_new); 168 | return -1; 169 | } 170 | 171 | int zlog_thread_rebuild_event(zlog_thread_t * a_thread, int time_cache_count) 172 | { 173 | zlog_event_t *event_new = NULL; 174 | zc_assert(a_thread, -1); 175 | 176 | event_new = zlog_event_new(time_cache_count); 177 | if (!event_new) { 178 | zc_error("zlog_event_new fail"); 179 | goto err; 180 | } 181 | 182 | zlog_event_del(a_thread->event); 183 | a_thread->event = event_new; 184 | return 0; 185 | err: 186 | if (event_new) zlog_event_del(event_new); 187 | return -1; 188 | } 189 | 190 | 191 | /*******************************************************************************/ 192 | -------------------------------------------------------------------------------- /src/thread.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zlog_thread_h 17 | #define __zlog_thread_h 18 | 19 | #include "zc_defs.h" 20 | #include "event.h" 21 | #include "buf.h" 22 | #include "mdc.h" 23 | 24 | typedef struct { 25 | int init_version; 26 | zlog_mdc_t *mdc; 27 | zlog_event_t *event; 28 | 29 | zlog_buf_t *pre_path_buf; 30 | zlog_buf_t *path_buf; 31 | zlog_buf_t *archive_path_buf; 32 | zlog_buf_t *pre_msg_buf; 33 | zlog_buf_t *msg_buf; 34 | } zlog_thread_t; 35 | 36 | 37 | void zlog_thread_del(zlog_thread_t * a_thread); 38 | void zlog_thread_profile(zlog_thread_t * a_thread, int flag); 39 | zlog_thread_t *zlog_thread_new(int init_version, 40 | size_t buf_size_min, size_t buf_size_max, int time_cache_count); 41 | 42 | int zlog_thread_rebuild_msg_buf(zlog_thread_t * a_thread, size_t buf_size_min, size_t buf_size_max); 43 | int zlog_thread_rebuild_event(zlog_thread_t * a_thread, int time_cache_count); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /src/version.h: -------------------------------------------------------------------------------- 1 | #define ZLOG_VERSION "1.2.12" 2 | -------------------------------------------------------------------------------- /src/zc_arraylist.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "zc_defs.h" 22 | 23 | zc_arraylist_t *zc_arraylist_new(zc_arraylist_del_fn del) 24 | { 25 | zc_arraylist_t *a_list; 26 | 27 | a_list = (zc_arraylist_t *) calloc(1, sizeof(zc_arraylist_t)); 28 | if (!a_list) { 29 | zc_error("calloc fail, errno[%d]", errno); 30 | return NULL; 31 | } 32 | a_list->size = ARRAY_LIST_DEFAULT_SIZE; 33 | a_list->len = 0; 34 | 35 | /* this could be NULL */ 36 | a_list->del = del; 37 | a_list->array = (void **)calloc(a_list->size, sizeof(void *)); 38 | if (!a_list->array) { 39 | zc_error("calloc fail, errno[%d]", errno); 40 | free(a_list); 41 | return NULL; 42 | } 43 | 44 | return a_list; 45 | } 46 | 47 | void zc_arraylist_del(zc_arraylist_t * a_list) 48 | { 49 | int i; 50 | 51 | if (!a_list) 52 | return; 53 | if (a_list->del) { 54 | for (i = 0; i < a_list->len; i++) { 55 | if (a_list->array[i]) 56 | a_list->del(a_list->array[i]); 57 | } 58 | } 59 | if (a_list->array) 60 | free(a_list->array); 61 | free(a_list); 62 | return; 63 | } 64 | 65 | static int zc_arraylist_expand_inner(zc_arraylist_t * a_list, int max) 66 | { 67 | void *tmp; 68 | int new_size; 69 | int diff_size; 70 | 71 | new_size = zc_max(a_list->size * 2, max); 72 | tmp = realloc(a_list->array, new_size * sizeof(void *)); 73 | if (!tmp) { 74 | zc_error("realloc fail, errno[%d]", errno); 75 | return -1; 76 | } 77 | a_list->array = (void **)tmp; 78 | diff_size = new_size - a_list->size; 79 | if (diff_size) memset(a_list->array + a_list->size, 0x00, diff_size * sizeof(void *)); 80 | a_list->size = new_size; 81 | return 0; 82 | } 83 | 84 | int zc_arraylist_set(zc_arraylist_t * a_list, int idx, void *data) 85 | { 86 | if (idx > a_list->size - 1) { 87 | if (zc_arraylist_expand_inner(a_list, idx)) { 88 | zc_error("expand_internal fail"); 89 | return -1; 90 | } 91 | } 92 | if (a_list->array[idx] && a_list->del) a_list->del(a_list->array[idx]); 93 | a_list->array[idx] = data; 94 | if (a_list->len <= idx) 95 | a_list->len = idx + 1; 96 | return 0; 97 | } 98 | 99 | int zc_arraylist_add(zc_arraylist_t * a_list, void *data) 100 | { 101 | return zc_arraylist_set(a_list, a_list->len, data); 102 | } 103 | 104 | /* assum idx < len */ 105 | static int zc_arraylist_insert_inner(zc_arraylist_t * a_list, int idx, 106 | void *data) 107 | { 108 | if (a_list->array[idx] == NULL) { 109 | a_list->array[idx] = data; 110 | return 0; 111 | } 112 | if (a_list->len > a_list->size - 1) { 113 | if (zc_arraylist_expand_inner(a_list, 0)) { 114 | zc_error("expand_internal fail"); 115 | return -1; 116 | } 117 | } 118 | memmove(a_list->array + idx + 1, a_list->array + idx, 119 | (a_list->len - idx) * sizeof(void *)); 120 | a_list->array[idx] = data; 121 | a_list->len++; 122 | return 0; 123 | } 124 | 125 | int zc_arraylist_sortadd(zc_arraylist_t * a_list, zc_arraylist_cmp_fn cmp, 126 | void *data) 127 | { 128 | int i; 129 | 130 | for (i = 0; i < a_list->len; i++) { 131 | if ((*cmp) (a_list->array[i], data) > 0) 132 | break; 133 | } 134 | 135 | if (i == a_list->len) 136 | return zc_arraylist_add(a_list, data); 137 | else 138 | return zc_arraylist_insert_inner(a_list, i, data); 139 | } 140 | -------------------------------------------------------------------------------- /src/zc_arraylist.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zc_arraylist_h 17 | #define __zc_arraylist_h 18 | 19 | #define ARRAY_LIST_DEFAULT_SIZE 32 20 | 21 | typedef void (*zc_arraylist_del_fn) (void *data); 22 | typedef int (*zc_arraylist_cmp_fn) (void *data1, void *data2); 23 | 24 | /* make zc_arraylist_foreach speed up, so keep struct defination here */ 25 | typedef struct { 26 | void **array; 27 | int len; 28 | int size; 29 | zc_arraylist_del_fn del; 30 | } zc_arraylist_t; 31 | 32 | zc_arraylist_t *zc_arraylist_new(zc_arraylist_del_fn del); 33 | void zc_arraylist_del(zc_arraylist_t * a_list); 34 | 35 | int zc_arraylist_set(zc_arraylist_t * a_list, int i, void *data); 36 | int zc_arraylist_add(zc_arraylist_t * a_list, void *data); 37 | int zc_arraylist_sortadd(zc_arraylist_t * a_list, zc_arraylist_cmp_fn cmp, 38 | void *data); 39 | 40 | #define zc_arraylist_len(a_list) (a_list->len) 41 | 42 | #define zc_arraylist_get(a_list, i) \ 43 | ((i >= a_list->len) ? NULL : a_list->array[i]) 44 | 45 | #define zc_arraylist_foreach(a_list, i, a_unit) \ 46 | for(i = 0, a_unit = a_list->array[0]; (i < a_list->len) && (a_unit = a_list->array[i], 1) ; i++) 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /src/zc_defs.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zc_defs_h 17 | #define __zc_defs_h 18 | 19 | #include "zc_profile.h" 20 | #include "zc_arraylist.h" 21 | #include "zc_hashtable.h" 22 | #include "zc_xplatform.h" 23 | #include "zc_util.h" 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /src/zc_hashtable.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | #include "zc_defs.h" 21 | #include "zc_hashtable.h" 22 | 23 | struct zc_hashtable_s { 24 | size_t nelem; 25 | 26 | zc_hashtable_entry_t **tab; 27 | size_t tab_size; 28 | 29 | zc_hashtable_hash_fn hash; 30 | zc_hashtable_equal_fn equal; 31 | zc_hashtable_del_fn key_del; 32 | zc_hashtable_del_fn value_del; 33 | }; 34 | 35 | zc_hashtable_t *zc_hashtable_new(size_t a_size, 36 | zc_hashtable_hash_fn hash, 37 | zc_hashtable_equal_fn equal, 38 | zc_hashtable_del_fn key_del, 39 | zc_hashtable_del_fn value_del) 40 | { 41 | zc_hashtable_t *a_table; 42 | 43 | a_table = calloc(1, sizeof(*a_table)); 44 | if (!a_table) { 45 | zc_error("calloc fail, errno[%d]", errno); 46 | return NULL; 47 | } 48 | 49 | a_table->tab = calloc(a_size, sizeof(*(a_table->tab))); 50 | if (!a_table->tab) { 51 | zc_error("calloc fail, errno[%d]", errno); 52 | free(a_table); 53 | return NULL; 54 | } 55 | a_table->tab_size = a_size; 56 | 57 | a_table->nelem = 0; 58 | a_table->hash = hash; 59 | a_table->equal = equal; 60 | 61 | /* these two could be NULL */ 62 | a_table->key_del = key_del; 63 | a_table->value_del = value_del; 64 | 65 | return a_table; 66 | } 67 | 68 | void zc_hashtable_del(zc_hashtable_t * a_table) 69 | { 70 | size_t i; 71 | zc_hashtable_entry_t *p; 72 | zc_hashtable_entry_t *q; 73 | 74 | if (!a_table) { 75 | zc_error("a_table[%p] is NULL, just do nothing", a_table); 76 | return; 77 | } 78 | 79 | for (i = 0; i < a_table->tab_size; i++) { 80 | for (p = (a_table->tab)[i]; p; p = q) { 81 | q = p->next; 82 | if (a_table->key_del) { 83 | a_table->key_del(p->key); 84 | } 85 | if (a_table->value_del) { 86 | a_table->value_del(p->value); 87 | } 88 | free(p); 89 | } 90 | } 91 | if (a_table->tab) 92 | free(a_table->tab); 93 | free(a_table); 94 | 95 | return; 96 | } 97 | 98 | void zc_hashtable_clean(zc_hashtable_t * a_table) 99 | { 100 | size_t i; 101 | zc_hashtable_entry_t *p; 102 | zc_hashtable_entry_t *q; 103 | 104 | for (i = 0; i < a_table->tab_size; i++) { 105 | for (p = (a_table->tab)[i]; p; p = q) { 106 | q = p->next; 107 | if (a_table->key_del) { 108 | a_table->key_del(p->key); 109 | } 110 | if (a_table->value_del) { 111 | a_table->value_del(p->value); 112 | } 113 | free(p); 114 | } 115 | (a_table->tab)[i] = NULL; 116 | } 117 | a_table->nelem = 0; 118 | return; 119 | } 120 | 121 | static int zc_hashtable_rehash(zc_hashtable_t * a_table) 122 | { 123 | size_t i; 124 | size_t j; 125 | size_t tab_size; 126 | zc_hashtable_entry_t **tab; 127 | zc_hashtable_entry_t *p; 128 | zc_hashtable_entry_t *q; 129 | 130 | tab_size = 2 * a_table->tab_size; 131 | tab = calloc(tab_size, sizeof(*tab)); 132 | if (!tab) { 133 | zc_error("calloc fail, errno[%d]", errno); 134 | return -1; 135 | } 136 | 137 | for (i = 0; i < a_table->tab_size; i++) { 138 | for (p = (a_table->tab)[i]; p; p = q) { 139 | q = p->next; 140 | 141 | p->next = NULL; 142 | p->prev = NULL; 143 | j = p->hash_key % tab_size; 144 | if (tab[j]) { 145 | tab[j]->prev = p; 146 | p->next = tab[j]; 147 | } 148 | tab[j] = p; 149 | } 150 | } 151 | free(a_table->tab); 152 | a_table->tab = tab; 153 | a_table->tab_size = tab_size; 154 | 155 | return 0; 156 | } 157 | 158 | zc_hashtable_entry_t *zc_hashtable_get_entry(zc_hashtable_t * a_table, const void *a_key) 159 | { 160 | unsigned int i; 161 | zc_hashtable_entry_t *p; 162 | 163 | i = a_table->hash(a_key) % a_table->tab_size; 164 | for (p = (a_table->tab)[i]; p; p = p->next) { 165 | if (a_table->equal(a_key, p->key)) 166 | return p; 167 | } 168 | 169 | return NULL; 170 | } 171 | 172 | void *zc_hashtable_get(zc_hashtable_t * a_table, const void *a_key) 173 | { 174 | unsigned int i; 175 | zc_hashtable_entry_t *p; 176 | 177 | i = a_table->hash(a_key) % a_table->tab_size; 178 | for (p = (a_table->tab)[i]; p; p = p->next) { 179 | if (a_table->equal(a_key, p->key)) 180 | return p->value; 181 | } 182 | 183 | return NULL; 184 | } 185 | 186 | int zc_hashtable_put(zc_hashtable_t * a_table, void *a_key, void *a_value) 187 | { 188 | int rc = 0; 189 | unsigned int i; 190 | zc_hashtable_entry_t *p = NULL; 191 | 192 | i = a_table->hash(a_key) % a_table->tab_size; 193 | for (p = (a_table->tab)[i]; p; p = p->next) { 194 | if (a_table->equal(a_key, p->key)) 195 | break; 196 | } 197 | 198 | if (p) { 199 | if (a_table->key_del) { 200 | a_table->key_del(p->key); 201 | } 202 | if (a_table->value_del) { 203 | a_table->value_del(p->value); 204 | } 205 | p->key = a_key; 206 | p->value = a_value; 207 | return 0; 208 | } else { 209 | if (a_table->nelem > a_table->tab_size * 1.3) { 210 | rc = zc_hashtable_rehash(a_table); 211 | if (rc) { 212 | zc_error("rehash fail"); 213 | return -1; 214 | } 215 | } 216 | 217 | p = calloc(1, sizeof(*p)); 218 | if (!p) { 219 | zc_error("calloc fail, errno[%d]", errno); 220 | return -1; 221 | } 222 | 223 | p->hash_key = a_table->hash(a_key); 224 | p->key = a_key; 225 | p->value = a_value; 226 | p->next = NULL; 227 | p->prev = NULL; 228 | 229 | i = p->hash_key % a_table->tab_size; 230 | if ((a_table->tab)[i]) { 231 | (a_table->tab)[i]->prev = p; 232 | p->next = (a_table->tab)[i]; 233 | } 234 | (a_table->tab)[i] = p; 235 | a_table->nelem++; 236 | } 237 | 238 | return 0; 239 | } 240 | 241 | void zc_hashtable_remove(zc_hashtable_t * a_table, const void *a_key) 242 | { 243 | zc_hashtable_entry_t *p; 244 | unsigned int i; 245 | 246 | if (!a_table || !a_key) { 247 | zc_error("a_table[%p] or a_key[%p] is NULL, just do nothing", a_table, a_key); 248 | return; 249 | } 250 | 251 | i = a_table->hash(a_key) % a_table->tab_size; 252 | for (p = (a_table->tab)[i]; p; p = p->next) { 253 | if (a_table->equal(a_key, p->key)) 254 | break; 255 | } 256 | 257 | if (!p) { 258 | zc_error("p[%p] not found in hashtable", p); 259 | return; 260 | } 261 | 262 | if (a_table->key_del) { 263 | a_table->key_del(p->key); 264 | } 265 | if (a_table->value_del) { 266 | a_table->value_del(p->value); 267 | } 268 | 269 | if (p->next) { 270 | p->next->prev = p->prev; 271 | } 272 | if (p->prev) { 273 | p->prev->next = p->next; 274 | } else { 275 | unsigned int i; 276 | 277 | i = p->hash_key % a_table->tab_size; 278 | a_table->tab[i] = p->next; 279 | } 280 | 281 | free(p); 282 | a_table->nelem--; 283 | 284 | return; 285 | } 286 | 287 | zc_hashtable_entry_t *zc_hashtable_begin(zc_hashtable_t * a_table) 288 | { 289 | size_t i; 290 | zc_hashtable_entry_t *p; 291 | 292 | for (i = 0; i < a_table->tab_size; i++) { 293 | for (p = (a_table->tab)[i]; p; p = p->next) { 294 | if (p) 295 | return p; 296 | } 297 | } 298 | 299 | return NULL; 300 | } 301 | 302 | zc_hashtable_entry_t *zc_hashtable_next(zc_hashtable_t * a_table, zc_hashtable_entry_t * a_entry) 303 | { 304 | size_t i; 305 | size_t j; 306 | 307 | if (a_entry->next) 308 | return a_entry->next; 309 | 310 | i = a_entry->hash_key % a_table->tab_size; 311 | 312 | for (j = i + 1; j < a_table->tab_size; j++) { 313 | if ((a_table->tab)[j]) { 314 | return (a_table->tab)[j]; 315 | } 316 | } 317 | 318 | return NULL; 319 | } 320 | 321 | /*******************************************************************************/ 322 | 323 | unsigned int zc_hashtable_str_hash(const void *str) 324 | { 325 | unsigned int h = 5381; 326 | const char *p = (const char *)str; 327 | 328 | while (*p != '\0') 329 | h = ((h << 5) + h) + (*p++); /* hash * 33 + c */ 330 | 331 | return h; 332 | } 333 | 334 | int zc_hashtable_str_equal(const void *key1, const void *key2) 335 | { 336 | return (STRCMP((const char *)key1, ==, (const char *)key2)); 337 | } 338 | -------------------------------------------------------------------------------- /src/zc_hashtable.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zc_hashtalbe_h 17 | #define __zc_hashtalbe_h 18 | 19 | #include 20 | 21 | typedef struct zc_hashtable_entry_s { 22 | unsigned int hash_key; 23 | void *key; 24 | void *value; 25 | struct zc_hashtable_entry_s *prev; 26 | struct zc_hashtable_entry_s *next; 27 | } zc_hashtable_entry_t; 28 | 29 | typedef struct zc_hashtable_s zc_hashtable_t; 30 | 31 | typedef unsigned int (*zc_hashtable_hash_fn) (const void *key); 32 | typedef int (*zc_hashtable_equal_fn) (const void *key1, const void *key2); 33 | typedef void (*zc_hashtable_del_fn) (void *kv); 34 | 35 | zc_hashtable_t *zc_hashtable_new(size_t a_size, 36 | zc_hashtable_hash_fn hash_fn, 37 | zc_hashtable_equal_fn equal_fn, 38 | zc_hashtable_del_fn key_del_fn, 39 | zc_hashtable_del_fn value_del_fn); 40 | 41 | void zc_hashtable_del(zc_hashtable_t * a_table); 42 | void zc_hashtable_clean(zc_hashtable_t * a_table); 43 | int zc_hashtable_put(zc_hashtable_t * a_table, void *a_key, void *a_value); 44 | zc_hashtable_entry_t *zc_hashtable_get_entry(zc_hashtable_t * a_table, const void *a_key); 45 | void *zc_hashtable_get(zc_hashtable_t * a_table, const void *a_key); 46 | void zc_hashtable_remove(zc_hashtable_t * a_table, const void *a_key); 47 | zc_hashtable_entry_t *zc_hashtable_begin(zc_hashtable_t * a_table); 48 | zc_hashtable_entry_t *zc_hashtable_next(zc_hashtable_t * a_table, zc_hashtable_entry_t * a_entry); 49 | 50 | #define zc_hashtable_foreach(a_table, a_entry) \ 51 | for(a_entry = zc_hashtable_begin(a_table); a_entry; a_entry = zc_hashtable_next(a_table, a_entry)) 52 | 53 | unsigned int zc_hashtable_str_hash(const void *str); 54 | int zc_hashtable_str_equal(const void *key1, const void *key2); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /src/zc_profile.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include "fmacros.h" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "zc_profile.h" 28 | #include "zc_xplatform.h" 29 | 30 | static void zc_time(char *time_str, size_t time_str_size) 31 | { 32 | time_t tt; 33 | 34 | time(&tt); 35 | #ifdef _WIN32 36 | struct tm *local_time; 37 | local_time = localtime(&tt); 38 | strftime(time_str, time_str_size, "%m-%d %H:%M:%S", local_time); 39 | #else 40 | struct tm local_time; 41 | localtime_r(&tt, &local_time); 42 | strftime(time_str, time_str_size, "%m-%d %H:%M:%S", &local_time); 43 | #endif 44 | 45 | return; 46 | } 47 | 48 | int zc_profile_inner(int flag, const char *file, const long line, const char *fmt, ...) 49 | { 50 | va_list args; 51 | char time_str[20 + 1]; 52 | FILE *fp = NULL; 53 | 54 | static char *debug_log = NULL; 55 | static char *error_log = NULL; 56 | static size_t init_flag = 0; 57 | 58 | if (!init_flag) { 59 | init_flag = 1; 60 | debug_log = getenv("ZLOG_PROFILE_DEBUG"); 61 | error_log = getenv("ZLOG_PROFILE_ERROR"); 62 | } 63 | 64 | switch (flag) { 65 | case ZC_DEBUG: 66 | if (debug_log == NULL) return 0; 67 | fp = fopen(debug_log, "a"); 68 | if (!fp) return -1; 69 | zc_time(time_str, sizeof(time_str)); 70 | fprintf(fp, "%s DEBUG (%d:%s:%ld) ", time_str, getpid(), file, line); 71 | break; 72 | case ZC_WARN: 73 | if (error_log == NULL) return 0; 74 | fp = fopen(error_log, "a"); 75 | if (!fp) return -1; 76 | zc_time(time_str, sizeof(time_str)); 77 | fprintf(fp, "%s WARN (%d:%s:%ld) ", time_str, getpid(), file, line); 78 | break; 79 | case ZC_ERROR: 80 | if (error_log == NULL) return 0; 81 | fp = fopen(error_log, "a"); 82 | if (!fp) return -1; 83 | zc_time(time_str, sizeof(time_str)); 84 | fprintf(fp, "%s ERROR (%d:%s:%ld) ", time_str, getpid(), file, line); 85 | break; 86 | } 87 | 88 | /* writing file twice(time & msg) is not atomic 89 | * may cause cross 90 | * but avoid log size limit */ 91 | va_start(args, fmt); 92 | vfprintf(fp, fmt, args); 93 | va_end(args); 94 | fprintf(fp, "\n"); 95 | 96 | fclose(fp); 97 | return 0; 98 | } 99 | 100 | -------------------------------------------------------------------------------- /src/zc_profile.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef __zc_profile_h 17 | #define __zc_profile_h 18 | 19 | #include 20 | 21 | #define EMPTY() 22 | #define zc_assert(expr, rv) \ 23 | if(!(expr)) { \ 24 | zc_error(#expr" is null or 0"); \ 25 | return rv; \ 26 | } 27 | 28 | enum zc_profile_flag { 29 | ZC_DEBUG = 0, 30 | ZC_WARN = 1, 31 | ZC_ERROR = 2 32 | }; 33 | 34 | 35 | #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L 36 | #define zc_debug(...) \ 37 | zc_profile_inner(ZC_DEBUG, __FILE__, __LINE__, __VA_ARGS__) 38 | #define zc_warn(...) \ 39 | zc_profile_inner(ZC_WARN, __FILE__, __LINE__, __VA_ARGS__) 40 | #define zc_error(...) \ 41 | zc_profile_inner(ZC_ERROR, __FILE__, __LINE__, __VA_ARGS__) 42 | #define zc_profile(flag, ...) \ 43 | zc_profile_inner(flag, __FILE__, __LINE__, __VA_ARGS__) 44 | #elif defined __GNUC__ 45 | #define zc_debug(fmt, args...) \ 46 | zc_profile_inner(ZC_DEBUG, __FILE__, __LINE__, fmt, ## args) 47 | #define zc_warn(fmt, args...) \ 48 | zc_profile_inner(ZC_WARN, __FILE__, __LINE__, fmt, ## args) 49 | #define zc_error(fmt, args...) \ 50 | zc_profile_inner(ZC_ERROR, __FILE__, __LINE__, fmt, ## args) 51 | #define zc_profile(flag, fmt, args...) \ 52 | zc_profile_inner(flag, __FILE__, __LINE__, fmt, ## args) 53 | #endif 54 | 55 | 56 | int zc_profile_inner(int flag, 57 | const char *file, const long line, 58 | const char *fmt, ...); 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /src/zc_util.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zc_defs.h" 23 | 24 | size_t zc_parse_byte_size(char *astring) 25 | { 26 | /* Parse size in bytes depending on the suffix. Valid suffixes are KB, MB and GB */ 27 | char *p; 28 | char *q; 29 | size_t sz; 30 | long res; 31 | int c, m; 32 | 33 | zc_assert(astring, 0); 34 | 35 | /* clear space */ 36 | for (p = q = astring; *p != '\0'; p++) { 37 | if (isspace(*p)) { 38 | continue; 39 | } else { 40 | *q = *p; 41 | q++; 42 | } 43 | } 44 | *q = '\0'; 45 | 46 | sz = strlen(astring); 47 | res = strtol(astring, (char **)NULL, 10); 48 | 49 | if (res <= 0) 50 | return 0; 51 | 52 | if (astring[sz - 1] == 'B' || astring[sz - 1] == 'b') { 53 | c = astring[sz - 2]; 54 | m = 1024; 55 | } else { 56 | c = astring[sz - 1]; 57 | m = 1000; 58 | } 59 | 60 | switch (c) { 61 | case 'K': 62 | case 'k': 63 | res *= m; 64 | break; 65 | case 'M': 66 | case 'm': 67 | res *= m * m; 68 | break; 69 | case 'G': 70 | case 'g': 71 | res *= m * m * m; 72 | break; 73 | default: 74 | if (!isdigit(c)) { 75 | zc_error("Wrong suffix parsing " "size in bytes for string [%s], ignoring suffix", 76 | astring); 77 | } 78 | break; 79 | } 80 | 81 | return (res); 82 | } 83 | 84 | /*******************************************************************************/ 85 | int zc_str_replace_env(char *str, size_t str_size) 86 | { 87 | char *p; 88 | char *q; 89 | char fmt[MAXLEN_CFG_LINE + 1]; 90 | char env_key[MAXLEN_CFG_LINE + 1]; 91 | char env_value[MAXLEN_CFG_LINE + 1]; 92 | int str_len; 93 | int env_value_len; 94 | int nscan; 95 | int nread; 96 | 97 | str_len = strlen(str); 98 | q = str; 99 | 100 | do { 101 | p = strchr(q, '%'); 102 | if (!p) { 103 | /* can't find more % */ 104 | break; 105 | } 106 | 107 | memset(fmt, 0x00, sizeof(fmt)); 108 | memset(env_key, 0x00, sizeof(env_key)); 109 | memset(env_value, 0x00, sizeof(env_value)); 110 | nread = 0; 111 | nscan = sscanf(p + 1, "%[.0-9-]%n", fmt + 1, &nread); 112 | if (nscan == 1) { 113 | fmt[0] = '%'; 114 | fmt[nread + 1] = 's'; 115 | } else { 116 | nread = 0; 117 | strcpy(fmt, "%s"); 118 | } 119 | 120 | q = p + 1 + nread; 121 | 122 | nscan = sscanf(q, "E(%[^)])%n", env_key, &nread); 123 | if (nscan == 0) { 124 | continue; 125 | } 126 | 127 | q += nread; 128 | 129 | if (*(q - 1) != ')') { 130 | zc_error("in string[%s] can't find match )", p); 131 | return -1; 132 | } 133 | 134 | env_value_len = snprintf(env_value, sizeof(env_value), fmt, getenv(env_key)); 135 | if (env_value_len < 0 || env_value_len >= sizeof(env_value)) { 136 | zc_error("snprintf fail, errno[%d], evn_value_len[%d]", 137 | errno, env_value_len); 138 | return -1; 139 | } 140 | 141 | str_len = str_len - (q - p) + env_value_len; 142 | if (str_len > str_size - 1) { 143 | zc_error("repalce env_value[%s] cause overlap", env_value); 144 | return -1; 145 | } 146 | 147 | memmove(p + env_value_len, q, strlen(q) + 1); 148 | memcpy(p, env_value, env_value_len); 149 | 150 | } while (1); 151 | 152 | return 0; 153 | } 154 | -------------------------------------------------------------------------------- /src/zc_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef __zc_util_h 16 | #define __zc_util_h 17 | 18 | size_t zc_parse_byte_size(char *astring); 19 | int zc_str_replace_env(char *str, size_t str_size); 20 | 21 | #define zc_max(a,b) ((a) > (b) ? (a) : (b)) 22 | #define zc_min(a,b) ((a) < (b) ? (a) : (b)) 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/zc_xplatform.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef __zc_xplatform_h 16 | #define __zc_xplatform_h 17 | 18 | #include 19 | 20 | #define ZLOG_INT32_LEN sizeof("-2147483648") - 1 21 | #define ZLOG_INT64_LEN sizeof("-9223372036854775808") - 1 22 | 23 | #if ((__GNU__ == 2) && (__GNUC_MINOR__ < 8)) 24 | #define ZLOG_MAX_UINT32_VALUE (uint32_t) 0xffffffffLL 25 | #else 26 | #define ZLOG_MAX_UINT32_VALUE (uint32_t) 0xffffffff 27 | #endif 28 | 29 | #define ZLOG_MAX_INT32_VALUE (uint32_t) 0x7fffffff 30 | 31 | #define MAXLEN_PATH 1024 32 | #define MAXLEN_CFG_LINE (MAXLEN_PATH * 4) 33 | #define MAXLINES_NO 128 34 | 35 | #define FILE_NEWLINE "\n" 36 | #define FILE_NEWLINE_LEN 1 37 | 38 | #include 39 | #include 40 | 41 | #define STRCMP(_a_,_C_,_b_) ( strcmp(_a_,_b_) _C_ 0 ) 42 | #define STRNCMP(_a_,_C_,_b_,_n_) ( strncmp(_a_,_b_,_n_) _C_ 0 ) 43 | #define STRICMP(_a_,_C_,_b_) ( strcasecmp(_a_,_b_) _C_ 0 ) 44 | #define STRNICMP(_a_,_C_,_b_,_n_) ( strncasecmp(_a_,_b_,_n_) _C_ 0 ) 45 | 46 | 47 | #ifdef __APPLE__ 48 | #include 49 | #endif 50 | 51 | /* Define zlog_fstat to fstat or fstat64() */ 52 | #if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6) 53 | #define zlog_fstat fstat64 54 | #define zlog_stat stat64 55 | #elif defined(_WIN32) 56 | #define zlog_fstat _fstat 57 | #define zlog_stat _stat 58 | #else 59 | #define zlog_fstat fstat 60 | #define zlog_stat stat 61 | #endif 62 | 63 | /* Define zlog_fsync to fdatasync() in Linux and fsync() for all the rest */ 64 | #ifdef __linux__ 65 | #define zlog_fsync fdatasync 66 | #else 67 | #define zlog_fsync fsync 68 | #endif 69 | 70 | 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /src/zlog-chk-conf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include "fmacros.h" 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | #include "zlog.h" 26 | #include "version.h" 27 | 28 | 29 | int main(int argc, char *argv[]) 30 | { 31 | int rc = 0; 32 | int op; 33 | int quiet = 0; 34 | static const char *help = 35 | "usage: zlog-chk-conf [conf files]...\n" 36 | "\t-q,\tsuppress non-error message\n" 37 | "\t-h,\tshow help message\n" 38 | "zlog version: " ZLOG_VERSION "\n"; 39 | 40 | while((op = getopt(argc, argv, "qhv")) > 0) { 41 | if (op == 'h') { 42 | fputs(help, stdout); 43 | return 0; 44 | } else if (op == 'q') { 45 | quiet = 1; 46 | } 47 | } 48 | 49 | argc -= optind; 50 | argv += optind; 51 | 52 | if (argc == 0) { 53 | fputs(help, stdout); 54 | return -1; 55 | } 56 | 57 | setenv("ZLOG_PROFILE_ERROR", "/dev/stderr", 1); 58 | setenv("ZLOG_CHECK_FORMAT_RULE", "1", 1); 59 | 60 | while (argc > 0) { 61 | rc = zlog_init(*argv); 62 | if (rc) { 63 | printf("\n---[%s] syntax error, see error message above\n", 64 | *argv); 65 | exit(2); 66 | } else { 67 | zlog_fini(); 68 | if (!quiet) { 69 | printf("--[%s] syntax right\n", *argv); 70 | } 71 | } 72 | argc--; 73 | argv++; 74 | } 75 | 76 | exit(0); 77 | } 78 | -------------------------------------------------------------------------------- /src/zlog_win.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "zlog_win.h" 8 | #include 9 | #include 10 | 11 | int gethostname_w(char *name, size_t len) 12 | { 13 | int rc = gethostname(name, len); 14 | DWORD newlen = len; 15 | 16 | if (rc != 0) { 17 | rc = GetComputerNameEx(ComputerNameDnsHostname, name, &newlen); 18 | if (rc == 0) { 19 | sprintf(name, "noname"); 20 | } 21 | } 22 | return 0; 23 | } 24 | #ifndef strcasecmp 25 | int strcasecmp (const char *sz1, const char *sz2) 26 | { 27 | return stricmp (sz1, sz2); 28 | } 29 | #endif 30 | #ifndef localtime_r 31 | struct tm *localtime_r(const time_t *timep, struct tm *result) 32 | { 33 | struct tm *ret = localtime(timep); 34 | if (ret) 35 | { 36 | memcpy(result, ret, sizeof(struct tm)); 37 | } 38 | return ret; 39 | } 40 | #endif 41 | int fsync (int fd) 42 | { 43 | HANDLE h = (HANDLE) _get_osfhandle (fd); 44 | DWORD err; 45 | 46 | if (h == INVALID_HANDLE_VALUE) 47 | { 48 | errno = EBADF; 49 | return -1; 50 | } 51 | 52 | if (!FlushFileBuffers (h)) 53 | { 54 | /* Translate some Windows errors into rough approximations of Unix 55 | * errors. MSDN is useless as usual - in this case it doesn't 56 | * document the full range of errors. 57 | */ 58 | err = GetLastError (); 59 | switch (err) 60 | { 61 | /* eg. Trying to fsync a tty. */ 62 | case ERROR_INVALID_HANDLE: 63 | errno = EINVAL; 64 | break; 65 | default: 66 | errno = EIO; 67 | } 68 | return -1; 69 | } 70 | return 0; 71 | } 72 | 73 | void setenv(const char *name, const char *value) 74 | { 75 | #ifdef HAVE_SETENV 76 | setenv(name, value, 1); 77 | #else 78 | int len = strlen(value)+1+strlen(value)+1; 79 | char *str = malloc(len); 80 | sprintf(str, "%s=%s", name, value); 81 | putenv(str); 82 | #endif 83 | } 84 | -------------------------------------------------------------------------------- /src/zlog_win.h: -------------------------------------------------------------------------------- 1 | #ifndef _ZLOG_WIN_H_ 2 | #define _ZLOG_WIN_H_ 3 | 4 | #include 5 | 6 | #ifndef localtime_r 7 | struct tm *localtime_r(const time_t *timep, struct tm *result); 8 | #endif 9 | #ifndef strcasecmp 10 | int strcasecmp (const char *sz1, const char *sz2); 11 | #endif 12 | int fsync (int fd); 13 | void setenv(const char *name, const char *value); 14 | int gethostname_w(char *name, size_t len); 15 | 16 | /* facility codes */ 17 | #define LOG_KERN (0<<3) /* kernel messages */ 18 | #define LOG_USER (1<<3) /* random user-level messages */ 19 | #define LOG_MAIL (2<<3) /* mail system */ 20 | #define LOG_DAEMON (3<<3) /* system daemons */ 21 | #define LOG_AUTH (4<<3) /* authorization messages */ 22 | #define LOG_SYSLOG (5<<3) /* messages generated internally by syslogd */ 23 | #define LOG_LPR (6<<3) /* line printer subsystem */ 24 | #define LOG_NEWS (7<<3) /* network news subsystem */ 25 | #define LOG_UUCP (8<<3) /* UUCP subsystem */ 26 | #define LOG_CRON (9<<3) /* clock daemon */ 27 | #define LOG_AUTHPRIV (10<<3) /* authorization messages (private) */ 28 | /* Facility #10 clashes in DEC UNIX, where */ 29 | /* it's defined as LOG_MEGASAFE for AdvFS */ 30 | /* event logging. */ 31 | #define LOG_FTP (11<<3) /* ftp daemon */ 32 | #define LOG_NTP (12<<3) /* NTP subsystem */ 33 | #define LOG_SECURITY (13<<3) /* security subsystems (firewalling, etc.) */ 34 | #define LOG_CONSOLE (14<<3) /* /dev/console output */ 35 | 36 | /* other codes through 15 reserved for system use */ 37 | #define LOG_LOCAL0 (16<<3) /* reserved for local use */ 38 | #define LOG_LOCAL1 (17<<3) /* reserved for local use */ 39 | #define LOG_LOCAL2 (18<<3) /* reserved for local use */ 40 | #define LOG_LOCAL3 (19<<3) /* reserved for local use */ 41 | #define LOG_LOCAL4 (20<<3) /* reserved for local use */ 42 | #define LOG_LOCAL5 (21<<3) /* reserved for local use */ 43 | #define LOG_LOCAL6 (22<<3) /* reserved for local use */ 44 | #define LOG_LOCAL7 (23<<3) /* reserved for local use */ 45 | 46 | /* 47 | * Option flags for openlog. 48 | * 49 | * LOG_ODELAY no longer does anything. 50 | * LOG_NDELAY is the inverse of what it used to be. 51 | */ 52 | #define LOG_PID 0x01 /* log the pid with each message */ 53 | #define LOG_CONS 0x02 /* log on the console if errors in sending */ 54 | #define LOG_ODELAY 0x04 /* delay open until first syslog() (default) */ 55 | #define LOG_NDELAY 0x08 /* don't delay open */ 56 | #define LOG_NOWAIT 0x10 /* don't wait for console forks: DEPRECATED */ 57 | #define LOG_PERROR 0x20 /* log to stderr as well */ 58 | 59 | /* 60 | * priorities/facilities are encoded into a single 32-bit quantity, where the 61 | * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility 62 | * (0-big number). Both the priorities and the facilities map roughly 63 | * one-to-one to strings in the syslogd(8) source code. This mapping is 64 | * included in this file. 65 | * 66 | * priorities (these are ordered) 67 | */ 68 | #define LOG_EMERG 0 /* system is unusable */ 69 | #define LOG_ALERT 1 /* action must be taken immediately */ 70 | #define LOG_CRIT 2 /* critical conditions */ 71 | #define LOG_ERR 3 /* error conditions */ 72 | #define LOG_WARNING 4 /* warning conditions */ 73 | #define LOG_NOTICE 5 /* normal but significant condition */ 74 | #define LOG_INFO 6 /* informational */ 75 | #define LOG_DEBUG 7 /* debug-level messages */ 76 | 77 | #define LOG_PRIMASK 0x07 /* mask to extract priority part (internal) */ 78 | /* extract priority */ 79 | #define LOG_PRI(p) ((p) & LOG_PRIMASK) 80 | #define LOG_MAKEPRI(fac, pri) ((fac) | (pri)) 81 | 82 | #endif -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | include_directories("${CMAKE_HOME_DIRECTORY}/src") 3 | 4 | aux_source_directory(. SRCS) 5 | 6 | if (WIN32) 7 | #message(STATUS ${SRCS}) 8 | list(REMOVE_ITEM SRCS ./test_press_syslog.c) 9 | list(REMOVE_ITEM SRCS ./test_syslog.c) 10 | list(REMOVE_ITEM SRCS ./test_press_write.c) 11 | list(REMOVE_ITEM SRCS ./test_press_write2.c) 12 | list(REMOVE_ITEM SRCS ./test_press_zlog.c) 13 | list(REMOVE_ITEM SRCS ./test_press_zlog2.c) 14 | #message(STATUS ${SRCS}) 15 | endif () 16 | 17 | set(not_auto_add_test 18 | test_hello 19 | test_bitmap 20 | test_hex 21 | test_leak 22 | test_press_write 23 | test_press_write2 24 | test_press_zlog 25 | test_press_zlog2 26 | test_press_syslog 27 | test_syslog 28 | test_longlog 29 | ) 30 | 31 | foreach (test_src ${SRCS}) 32 | string(REGEX MATCH "^.*/([^/]+)[.]c$" test_name ${test_src}) 33 | set(test_name ${CMAKE_MATCH_1}) 34 | 35 | message(STATUS "${test_name} ${test_src}") 36 | 37 | add_executable("${test_name}" "${test_src}") 38 | target_link_libraries(${test_name} zlog) 39 | 40 | list(FIND not_auto_add_test ${test_name} not_auto_test) 41 | if (not_auto_test EQUAL -1) 42 | add_test("${test_name}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${test_name}") 43 | endif () 44 | endforeach (test_src) 45 | 46 | add_test(test_hello "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_hello" hello_output 3) 47 | add_test(test_longlog "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_longlog" 2222) 48 | add_test(test_bitmap "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_bitmap" 0xaa55 0x66) 49 | 50 | file(GLOB CONF_FILES . *.conf) 51 | file(COPY 52 | ${CONF_FILES} hello_output 53 | DESTINATION ${CMAKE_CURRENT_BINARY_DIR} 54 | ) 55 | 56 | -------------------------------------------------------------------------------- /test/fuzzers/zlog_init_fuzzer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "zlog.h" 5 | 6 | int 7 | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 8 | { 9 | char filename[256]; 10 | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); 11 | 12 | FILE *fp = fopen(filename, "wb"); 13 | if (!fp) 14 | return 0; 15 | fwrite(data, size, 1, fp); 16 | fclose(fp); 17 | 18 | int rc = zlog_init(filename); 19 | if (rc == 0) 20 | { 21 | zlog_fini(); 22 | } 23 | unlink(filename); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /test/hello_output: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HardySimpson/zlog/695e18341f7c7410a3e377f491053fcbfdd7fd59/test/hello_output -------------------------------------------------------------------------------- /test/makefile: -------------------------------------------------------------------------------- 1 | exe = \ 2 | test_tmp \ 3 | test_longlog \ 4 | test_buf \ 5 | test_bitmap \ 6 | test_conf \ 7 | test_conf2 \ 8 | test_hashtable \ 9 | test_hello \ 10 | test_hex \ 11 | test_init \ 12 | test_level \ 13 | test_leak \ 14 | test_mdc \ 15 | test_multithread \ 16 | test_record \ 17 | test_pipe \ 18 | test_press_zlog \ 19 | test_press_zlog2 \ 20 | test_press_write \ 21 | test_press_write2 \ 22 | test_press_syslog \ 23 | test_syslog \ 24 | test_default \ 25 | test_profile \ 26 | test_category \ 27 | test_prompt \ 28 | test_enabled 29 | 30 | all : $(exe) 31 | 32 | $(exe) : %:%.o 33 | gcc -O2 -g -o $@ $^ -L../src -lzlog -lpthread -Wl,-rpath ../src 34 | 35 | .c.o : 36 | gcc -O2 -g -Wall -D_GNU_SOURCE -o $@ -c $< -I. -I../src 37 | 38 | clean : 39 | rm -f press.log* *.o $(exe) 40 | 41 | .PHONY : clean all 42 | -------------------------------------------------------------------------------- /test/test_bitmap.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include "zlog.h" 20 | 21 | int main(int argc, char** argv) 22 | { 23 | unsigned char aa[32]; 24 | int i, j; 25 | 26 | if (argc != 3) { 27 | printf("useage: test_bitmap i j\n"); 28 | exit(1); 29 | } 30 | 31 | dzlog_init(NULL, "AA"); 32 | 33 | 34 | i = atoi(argv[1]); 35 | j = atoi(argv[2]); 36 | 37 | memset(aa, 0x00, sizeof(aa)); 38 | 39 | /* 32 byte, 256 bit 40 | * [11111..1100...00] 41 | * i 42 | */ 43 | aa[i/8] |= ~(0xFF << (8 - i % 8)); 44 | memset(aa + i/8 + 1, 0xFF, sizeof(aa) - i/8 - 1); 45 | 46 | hdzlog_info(aa, sizeof(aa)); 47 | 48 | dzlog_info("%0x", aa[j/8]); 49 | dzlog_info("%0x", aa[j/8] >> 6); 50 | 51 | /* see j of bits fits */ 52 | dzlog_info("%0x", ~((aa[j/8] >> (7 - j % 8)) & 0x01) ); 53 | 54 | zlog_fini(); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /test/test_buf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zc_defs.h" 18 | #include "buf.h" 19 | 20 | int main(int argc, char** argv) 21 | { 22 | zlog_buf_t *a_buf; 23 | char *aa; 24 | 25 | a_buf = zlog_buf_new(10, 20, "ABC"); 26 | if (!a_buf) { 27 | zc_error("zlog_buf_new fail"); 28 | return -1; 29 | } 30 | 31 | aa = "123456789"; 32 | zlog_buf_append(a_buf, aa, strlen(aa)); 33 | zc_error("a_buf->start[%s]", a_buf->start); 34 | fwrite(a_buf->start, zlog_buf_len(a_buf), 1, stdout); 35 | zc_error("------------"); 36 | 37 | aa = "0"; 38 | zlog_buf_append(a_buf, aa, strlen(aa)); 39 | zc_error("a_buf->start[%s]", a_buf->start); 40 | zc_error("------------"); 41 | 42 | aa = "12345"; 43 | zlog_buf_append(a_buf, aa, strlen(aa)); 44 | zc_error("a_buf->start[%s]", a_buf->start); 45 | zc_error("------------"); 46 | 47 | aa = "6789"; 48 | zlog_buf_append(a_buf, aa, strlen(aa)); 49 | zc_error("a_buf->start[%s]", a_buf->start); 50 | zc_error("------------"); 51 | 52 | aa = "0"; 53 | zlog_buf_append(a_buf, aa, strlen(aa)); 54 | zc_error("a_buf->start[%s]", a_buf->start); 55 | zc_error("------------"); 56 | 57 | aa = "22345"; 58 | zlog_buf_append(a_buf, aa, strlen(aa)); 59 | zc_error("a_buf->start[%s]", a_buf->start); 60 | zc_error("------------"); 61 | 62 | 63 | aa = "abc"; 64 | int i,j; 65 | for (i = 0; i <= 5; i++) { 66 | for (j = 0; j <= 5; j++) { 67 | zlog_buf_restart(a_buf); 68 | zc_error("left[1],max[%d],min[%d]", i, j); 69 | zlog_buf_adjust_append(a_buf, aa, strlen(aa), 1, 0, i, j); 70 | zc_error("a_buf->start[%s]", a_buf->start); 71 | 72 | zc_error("-----"); 73 | 74 | zlog_buf_restart(a_buf); 75 | zc_error("left[0],max[%d],min[%d]", i, j); 76 | zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 0, i, j); 77 | zc_error("a_buf->start[%s]", a_buf->start); 78 | zc_error("------------"); 79 | } 80 | } 81 | 82 | aa = "1234567890"; 83 | zc_error("left[0],max[%d],min[%d]", 15, 5); 84 | zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 0, 15, 5); 85 | zc_error("a_buf->start[%s]", a_buf->start); 86 | zc_error("------------"); 87 | 88 | aa = "1234567890"; 89 | zlog_buf_restart(a_buf); 90 | zc_error("left[0],max[%d],min[%d]", 25, 5); 91 | zlog_buf_adjust_append(a_buf, aa, strlen(aa), 1, 0, 25, 5); 92 | zc_error("a_buf->start[%s]", a_buf->start); 93 | zc_error("------------"); 94 | 95 | zlog_buf_restart(a_buf); 96 | zc_error("left[0],max[%d],min[%d]", 19, 5); 97 | zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 0, 19, 5); 98 | zc_error("a_buf->start[%s]", a_buf->start); 99 | zc_error("------------"); 100 | 101 | zlog_buf_restart(a_buf); 102 | zc_error("left[0],max[%d],min[%d]", 20, 5); 103 | zlog_buf_adjust_append(a_buf, aa, strlen(aa), 0, 0, 20, 5); 104 | zc_error("a_buf->start[%s]", a_buf->start); 105 | zc_error("------------"); 106 | 107 | zlog_buf_del(a_buf); 108 | 109 | return 0; 110 | } 111 | -------------------------------------------------------------------------------- /test/test_category.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | zlog_category_t *zc; 23 | 24 | rc = zlog_init("test_category.conf"); 25 | if (rc) { 26 | printf("init failed\n"); 27 | return -1; 28 | } 29 | 30 | zc = zlog_get_category("my_cat"); 31 | if (!zc) { 32 | printf("get cat fail\n"); 33 | zlog_fini(); 34 | return -2; 35 | } 36 | 37 | zlog_debug(zc, "hello, zlog - debug"); 38 | 39 | zc = zlog_get_category("my-cat"); 40 | if (!zc) { 41 | printf("get cat fail\n"); 42 | zlog_fini(); 43 | return -2; 44 | } 45 | 46 | zlog_info(zc, "hello, zlog - info"); 47 | 48 | zlog_fini(); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /test/test_category.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | default format = "%V %v %m%n" 3 | [rules] 4 | my_cat.* >stdout; 5 | my-cat.* >stdout; 6 | -------------------------------------------------------------------------------- /test/test_conf.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zlog.h" 23 | 24 | int main(int argc, char** argv) 25 | { 26 | int rc; 27 | zlog_category_t *zc; 28 | 29 | 30 | rc = zlog_init("test_conf.conf"); 31 | if (rc) { 32 | printf("init failed, try zlog-chk-conf test_conf.conf for more detail\n"); 33 | return -1; 34 | } 35 | 36 | zc = zlog_get_category("my_cat"); 37 | if (!zc) { 38 | printf("get cat fail\n"); 39 | zlog_fini(); 40 | return -2; 41 | } 42 | 43 | zlog_info(zc, "hello, zlog"); 44 | 45 | zlog_fini(); 46 | printf("log end\n"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /test/test_conf.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | buffer min= 1024 4 | buffer max= 2MB 5 | rotate lock file = /tmp/zlog.lock 6 | default format = "defalut - %d(%F %X.%ms) %-6V (%c:%F:%U:%L) - %m%n" 7 | 8 | [formats] 9 | null = "%n" 10 | print = "print - [%-10.3d(%F)]%n" 11 | 12 | date = "date start%n%d(%a--Wed)%n%d(%A--Wednesday)%n%d(%b--Mar)%n%d(%B--March)%n%d(%c--WedMar211:45:262011)%n%d(%C--20)%n%d(%d--02)%n%d(%D--03/02/11)%n%d(%e--2)%n%d(%F--2011-03-02)%n%d(%g--11)%n%d(%G--2011)%n%d(%h--Mar)%n%d(%H--11)%n%d(%I--11)%n%d(%j--061)%n%d(%k-k)%n%d(%l-l)%n%d(%ms--500)%n%d(%m--03)%n%d(%M--45)%n%d(%us--500730)%n%d(%p--AM)%n%d(%r--11:45:26AM)%n%d(%R--11:45)%n%d(%s--epoch)%n%d(%S--26)%n%d(%t--)%n%d(%T--11:45:26)%n%d(%u--3)%n%d(%U--09)%n%d(%V--09)%n%d(%w--3)%n%d(%W--09)%n%d(%x--03/02/11)%n%d(%X--11:45:26)%n%d(%y--11)%n%d(%Y--2011)%n%d(%z--+0800)%n%d(%Z--CST)%n%d(%%--%)%n%d(%J--%J)%ndate end%n" 13 | 14 | simple = "simple - %m%n" 15 | 16 | text = "text - text%n" 17 | 18 | ms = "ms - %d(%a--Wed)[%d(%ms)]%n" 19 | 20 | msus = "msus - %d(%ms,%us,%ms,%us)%n" 21 | 22 | [rules] 23 | *.* >stderr; 24 | *.* >stderr; null 25 | *.* >stderr; print 26 | *.* >stderr; date 27 | *.* >stderr; simple 28 | *.* >stderr; text 29 | *.* >stderr; ms 30 | *.* >stderr; msus 31 | -------------------------------------------------------------------------------- /test/test_conf2.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zlog.h" 23 | #include "test_conf2.conf.h" 24 | 25 | int main(int argc, char** argv) 26 | { 27 | int rc; 28 | zlog_category_t *zc; 29 | 30 | rc = zlog_init(test_conf2_conf); 31 | if (rc) { 32 | printf("init failed, save [] in a config file and try zlog-chk-conf for more detail [%s]\n", test_conf2_conf); 33 | return -1; 34 | } 35 | 36 | zc = zlog_get_category("my_cat"); 37 | if (!zc) { 38 | printf("get cat fail\n"); 39 | zlog_fini(); 40 | return -2; 41 | } 42 | 43 | zlog_info(zc, "hello, zlog"); 44 | 45 | zlog_fini(); 46 | printf("log end\n"); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /test/test_conf2.conf.h: -------------------------------------------------------------------------------- 1 | #define test_conf2_conf "[global]\nstrict init = true\nbuffer min= 1024\nbuffer max= 2MB\nrotate lock file = /tmp/zlog.lock\ndefault format = \"defalut - %d(%F %X.%ms) %-6V (%c:%F:%U:%L) - %m%n\"\n\n[formats]\nnull = \"%n\"\nprint = \"print - [%-10.3d(%F)]%n\"\n\ndate = \"date start%n%d(%a--Wed)%n%d(%A--Wednesday)%n%d(%b--Mar)%n%d(%B--March)%n%d(%c--WedMar211:45:262011)%n%d(%C--20)%n%d(%d--02)%n%d(%D--03/02/11)%n%d(%e--2)%n%d(%F--2011-03-02)%n%d(%g--11)%n%d(%G--2011)%n%d(%h--Mar)%n%d(%H--11)%n%d(%I--11)%n%d(%j--061)%n%d(%k-k)%n%d(%l-l)%n%d(%ms--500)%n%d(%m--03)%n%d(%M--45)%n%d(%us--500730)%n%d(%p--AM)%n%d(%r--11:45:26AM)%n%d(%R--11:45)%n%d(%s--epoch)%n%d(%S--26)%n%d(%t--)%n%d(%T--11:45:26)%n%d(%u--3)%n%d(%U--09)%n%d(%V--09)%n%d(%w--3)%n%d(%W--09)%n%d(%x--03/02/11)%n%d(%X--11:45:26)%n%d(%y--11)%n%d(%Y--2011)%n%d(%z--+0800)%n%d(%Z--CST)%n%d(%%--%)%n%d(%J--%J)%ndate end%n\"\n\nsimple = \"simple - %m%n\"\n\ntext = \"text - text%n\"\n\nms = \"ms - %d(%a--Wed)[%d(%ms)]%n\"\n\nmsus = \"msus - %d(%ms,%us,%ms,%us)%n\"\n\n[rules]\n*.* >stderr;\n*.* >stderr; null\n*.* >stderr; print\n*.* >stderr; date\n*.* >stderr; simple\n*.* >stderr; text\n*.* >stderr; ms\n*.* >stderr; msus" 2 | -------------------------------------------------------------------------------- /test/test_default.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | 23 | rc = dzlog_init("test_default.conf", "my_cat"); 24 | if (rc) { 25 | printf("init failed\n"); 26 | return -1; 27 | } 28 | 29 | dzlog_info("hello, zlog"); 30 | 31 | zlog_fini(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /test/test_default.conf: -------------------------------------------------------------------------------- 1 | [rules] 2 | my_cat.* >stdout 3 | -------------------------------------------------------------------------------- /test/test_enabled.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "test_enabled.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | zlog_category_t *zc; 23 | 24 | rc = zlog_init("test_enabled.conf"); 25 | if (rc) { 26 | printf("init failed\n"); 27 | return -1; 28 | } 29 | 30 | zc = zlog_get_category("my_cat"); 31 | if (!zc) { 32 | printf("get cat fail\n"); 33 | zlog_fini(); 34 | return -2; 35 | } 36 | 37 | if (zlog_trace_enabled(zc)) { 38 | /* do something heavy to collect data */ 39 | zlog_trace(zc, "hello, zlog - trace"); 40 | } 41 | 42 | if (zlog_debug_enabled(zc)) { 43 | /* do something heavy to collect data */ 44 | zlog_debug(zc, "hello, zlog - debug"); 45 | } 46 | 47 | if (zlog_info_enabled(zc)) { 48 | /* do something heavy to collect data */ 49 | zlog_info(zc, "hello, zlog - info"); 50 | } 51 | 52 | zlog_fini(); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /test/test_enabled.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | default format = "%V %v %m%n" 3 | [levels] 4 | TRACE = 30, LOG_DEBUG 5 | [rules] 6 | my_cat.TRACE >stdout; 7 | -------------------------------------------------------------------------------- /test/test_enabled.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the zlog Library. 3 | * 4 | * Copyright (C) 2018 by Teracom Telemática S/A 5 | * 6 | * The zlog Library is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The zlog Library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with the zlog Library. If not, see . 18 | */ 19 | 20 | #ifndef __test_level_h 21 | #define __test_level_h 22 | 23 | #include "zlog.h" 24 | 25 | enum { 26 | ZLOG_LEVEL_TRACE = 30, 27 | /* must equals conf file setting */ 28 | }; 29 | 30 | #define zlog_trace(cat, format, args...) \ 31 | zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \ 32 | ZLOG_LEVEL_TRACE, format, ##args) 33 | 34 | #define zlog_trace_enabled(cat) zlog_level_enabled(cat, ZLOG_LEVEL_TRACE) 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /test/test_hashtable.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zc_profile.c" 23 | #include "zc_hashtable.h" 24 | #include "zc_hashtable.c" 25 | 26 | void myfree(void *kv) 27 | { 28 | } 29 | 30 | int main(void) 31 | { 32 | zc_hashtable_t *a_table; 33 | zc_hashtable_entry_t *a_entry; 34 | 35 | a_table = zc_hashtable_new(20, 36 | zc_hashtable_str_hash, 37 | zc_hashtable_str_equal, 38 | myfree, myfree); 39 | 40 | zc_hashtable_put(a_table, "aaa", "bnbb"); 41 | zc_hashtable_put(a_table, "bbb", "bnbb"); 42 | zc_hashtable_put(a_table, "ccc", "bnbb"); 43 | 44 | zc_hashtable_put(a_table, "aaa", "123"); 45 | 46 | zc_hashtable_foreach(a_table, a_entry) { 47 | printf("k[%s],v[%s]\n", (char*)a_entry->key, (char*)a_entry->value); 48 | } 49 | 50 | printf("getv[%s]\n", (char*)zc_hashtable_get(a_table, "ccc")); 51 | 52 | zc_hashtable_remove(a_table, "ccc"); 53 | 54 | zc_hashtable_foreach(a_table, a_entry) { 55 | printf("k[%s],v[%s]\n", (char*)a_entry->key, (char*)a_entry->value); 56 | } 57 | 58 | 59 | zc_hashtable_remove(a_table, NULL); 60 | zc_hashtable_del(NULL); 61 | 62 | zc_hashtable_del(a_table); 63 | return 0; 64 | } 65 | 66 | -------------------------------------------------------------------------------- /test/test_hello.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | zlog_category_t *zc; 23 | 24 | rc = zlog_init("test_hello.conf"); 25 | if (rc) { 26 | printf("init failed\n"); 27 | return -1; 28 | } 29 | 30 | zc = zlog_get_category("my_cat"); 31 | if (!zc) { 32 | printf("get cat fail\n"); 33 | zlog_fini(); 34 | return -2; 35 | } 36 | 37 | zlog_info(zc, "hello, zlog"); 38 | 39 | zlog_fini(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /test/test_hello.conf: -------------------------------------------------------------------------------- 1 | [formats] 2 | simple = "%d.%ms %m%n" 3 | simple2 = "%d.%us %m%n" 4 | [rules] 5 | my_cat.* >stderr; 6 | my_cat.* >stdout;simple 7 | my_cat.* >stdout;simple2 8 | -------------------------------------------------------------------------------- /test/test_hex.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zlog.h" 23 | #include "stdlib.h" 24 | 25 | static int ReadTotalFile( FILE * fp , char ** ptr , long * len ) 26 | { 27 | long fileLen ; 28 | int nret ; 29 | char * pStart ; 30 | 31 | *ptr = NULL; 32 | 33 | nret = fseek( fp , 0L , SEEK_END ); 34 | if( nret ) 35 | { 36 | return -1; 37 | } 38 | 39 | fileLen = ftell( fp ); 40 | if( fileLen < 0 ) 41 | { 42 | return -2; 43 | } 44 | 45 | if( ( pStart = calloc(1, fileLen+1) ) == NULL ) 46 | { 47 | return -3; 48 | } 49 | 50 | nret = fseek( fp , 0L , SEEK_SET ); 51 | if( nret ) 52 | { 53 | free( pStart ); 54 | return -4; 55 | } 56 | 57 | nret = fread( pStart , fileLen , 1 , fp ); 58 | if( ferror( fp ) ) 59 | { 60 | free( pStart ); 61 | return -5; 62 | } 63 | 64 | *ptr = pStart; 65 | *len = fileLen; 66 | 67 | return 0; 68 | } 69 | 70 | int main(int argc, char** argv) 71 | { 72 | int rc; 73 | 74 | FILE *fp; 75 | char *dmp; 76 | long dmp_len = 0; 77 | int ntimes; 78 | 79 | if (argc != 3) { 80 | printf("useage: test_hex [file] [ntimes]\n"); 81 | exit(1); 82 | } 83 | 84 | fp = fopen(argv[1], "r"); 85 | if (!fp) { 86 | printf("fopen[%s] fail\n", argv[1]); 87 | exit(1); 88 | } 89 | 90 | ntimes = atoi(argv[2]); 91 | 92 | zlog_category_t *zc; 93 | 94 | rc = zlog_init("test_hex.conf"); 95 | if (rc) { 96 | printf("init failed\n"); 97 | return -1; 98 | } 99 | 100 | zc = zlog_get_category("my_cat"); 101 | if (!zc) { 102 | printf("get category failed\n"); 103 | zlog_fini(); 104 | return -2; 105 | } 106 | 107 | 108 | rc = ReadTotalFile(fp, &dmp, &dmp_len); 109 | 110 | while(ntimes--) hzlog_debug(zc, dmp, dmp_len); 111 | 112 | fclose(fp); 113 | free(dmp); 114 | 115 | zlog_fini(); 116 | printf("hex log end\n"); 117 | 118 | return 0; 119 | } 120 | -------------------------------------------------------------------------------- /test/test_hex.conf: -------------------------------------------------------------------------------- 1 | [rules] 2 | *.* "hex.log"; 3 | -------------------------------------------------------------------------------- /test/test_init.2.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | buffer min = 2048 3 | buffer max = 4096 4 | default format = "%V %m%n" 5 | [levels] 6 | TEST = 40, LOG_INFO 7 | 8 | 9 | [formats] 10 | simple = "%m%n" 11 | [rules] 12 | my_cat.* >stderr; 13 | -------------------------------------------------------------------------------- /test/test_init.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zlog.h" 23 | 24 | int main(int argc, char** argv) 25 | { 26 | int rc; 27 | 28 | zlog_category_t *zc; 29 | 30 | rc = zlog_init("test_init.conf"); 31 | if (rc) { 32 | printf("init fail"); 33 | return -2; 34 | } 35 | zc = zlog_get_category("my_cat"); 36 | if (!zc) { 37 | printf("zlog_get_category fail\n"); 38 | zlog_fini(); 39 | return -1; 40 | } 41 | zlog_info(zc, "before update"); 42 | sleep(1); 43 | rc = zlog_reload("test_init.2.conf"); 44 | if (rc) { 45 | printf("update fail\n"); 46 | } 47 | zlog_info(zc, "after update"); 48 | zlog_profile(); 49 | zlog_fini(); 50 | 51 | sleep(1); 52 | zlog_init("test_init.conf"); 53 | zc = zlog_get_category("my_cat"); 54 | if (!zc) { 55 | printf("zlog_get_category fail\n"); 56 | zlog_fini(); 57 | return -1; 58 | } 59 | zlog_info(zc, "init again"); 60 | zlog_fini(); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /test/test_init.conf: -------------------------------------------------------------------------------- 1 | [ global ] 2 | strict init = true 3 | buffer min = 1024 4 | buffer max = 2MB 5 | rotate lock file= /tmp/zlog.lock 6 | 7 | [ rules ] 8 | my_cat.* >stderr; 9 | -------------------------------------------------------------------------------- /test/test_leak.2.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | buffer min = 100 4 | buffer max = 200 5 | default format = "%m%n" 6 | 7 | [rules ] 8 | *.* >stdout; 9 | -------------------------------------------------------------------------------- /test/test_leak.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zlog.h" 23 | 24 | int main(int argc, char** argv) 25 | { 26 | int rc; 27 | int k; 28 | int i; 29 | 30 | if (argc != 2) { 31 | printf("test_leak ntime\n"); 32 | return -1; 33 | } 34 | 35 | rc = zlog_init("test_leak.conf"); 36 | 37 | k = atoi(argv[1]); 38 | while (k-- > 0) { 39 | i = rand(); 40 | switch (i % 4) { 41 | case 0: 42 | rc = dzlog_init("test_leak.conf", "xxx"); 43 | dzlog_info("init, rc=[%d]", rc); 44 | break; 45 | case 1: 46 | rc = zlog_reload(NULL); 47 | dzlog_info("reload null, rc=[%d]", rc); 48 | break; 49 | case 2: 50 | rc = zlog_reload("test_leak.2.conf"); 51 | dzlog_info("reload 2, rc=[%d]", rc); 52 | break; 53 | case 3: 54 | zlog_fini(); 55 | printf("fini\n"); 56 | // printf("zlog_finish\tj=[%d], rc=[%d]\n", j, rc); 57 | break; 58 | } 59 | } 60 | 61 | zlog_fini(); 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /test/test_leak.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | buffer min = 2048 4 | buffer max = 4096 5 | rotate lock file = /tmp/zlog.lock 6 | default format = "%d(%F %T).%ms %-6V (%c:%F:%L) - %m%n" 7 | 8 | [ levels ] 9 | TEST = 40, LOG_INFO 10 | 11 | [rules ] 12 | *.* >stderr; 13 | -------------------------------------------------------------------------------- /test/test_level.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "test_level.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | zlog_category_t *zc; 23 | 24 | rc = zlog_init("test_level.conf"); 25 | if (rc) { 26 | printf("init failed\n"); 27 | return -1; 28 | } 29 | 30 | zc = zlog_get_category("my_cat"); 31 | if (!zc) { 32 | printf("get cat fail\n"); 33 | zlog_fini(); 34 | return -2; 35 | } 36 | 37 | zlog_trace(zc, "hello, zlog - trace"); 38 | zlog_debug(zc, "hello, zlog - debug"); 39 | zlog_info(zc, "hello, zlog - info"); 40 | 41 | zlog_fini(); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /test/test_level.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | default format = "%V %v %m%n" 3 | [levels] 4 | TRACE = 30, LOG_DEBUG 5 | [rules] 6 | my_cat.TRACE >stdout; 7 | -------------------------------------------------------------------------------- /test/test_level.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the zlog Library. 3 | * 4 | * Copyright (C) 2011 by Hardy Simpson 5 | * 6 | * The zlog Library is free software: you can redistribute it and/or modify 7 | * it under the terms of the GNU Lesser General Public License as published by 8 | * the Free Software Foundation, either version 3 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * The zlog Library is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with the zlog Library. If not, see . 18 | */ 19 | 20 | #ifndef __test_level_h 21 | #define __test_level_h 22 | 23 | #include "zlog.h" 24 | 25 | enum { 26 | ZLOG_LEVEL_TRACE = 30, 27 | /* must equals conf file setting */ 28 | }; 29 | 30 | #define zlog_trace(cat, format, args...) \ 31 | zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \ 32 | ZLOG_LEVEL_TRACE, format, ##args) 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /test/test_longlog.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | #include 19 | #include 20 | 21 | #define str "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" 22 | #define str2 str str 23 | #define str4 str2 str2 24 | #define str8 str4 str4 25 | #define str16 str8 str8 26 | #define str32 str16 str16 27 | #define str64 str32 str32 28 | 29 | int main(int argc, char** argv) 30 | { 31 | int i, k; 32 | int rc; 33 | zlog_category_t *zc; 34 | 35 | if (argc != 2) { 36 | printf("useage: test_longlog [count]\n"); 37 | exit(1); 38 | } 39 | 40 | rc = zlog_init("test_longlog.conf"); 41 | if (rc) { 42 | printf("init failed\n"); 43 | return -1; 44 | } 45 | 46 | zc = zlog_get_category("my_cat"); 47 | if (!zc) { 48 | printf("get cat fail\n"); 49 | zlog_fini(); 50 | return -2; 51 | } 52 | 53 | k = atoi(argv[1]); 54 | while (k-- > 0) { 55 | i = rand(); 56 | switch (i % 3) { 57 | case 0: 58 | zlog_info(zc, str32); 59 | break; 60 | case 1: 61 | zlog_info(zc, str64); 62 | break; 63 | case 2: 64 | zlog_info(zc, str16); 65 | break; 66 | } 67 | } 68 | 69 | 70 | zlog_fini(); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /test/test_longlog.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | #buffer min = 1024 4 | #buffer max = 0 5 | rotate lock file = /tmp/zlog.lock 6 | 7 | [formats] 8 | simple = "%d %V %m%n" 9 | simple2 = "%d %V %m%n" 10 | 11 | [rules ] 12 | *.* "test_longlog.log" 13 | -------------------------------------------------------------------------------- /test/test_mdc.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "zlog.h" 23 | 24 | int main(int argc, char** argv) 25 | { 26 | int rc; 27 | zlog_category_t *zc; 28 | 29 | rc = zlog_init("test_mdc.conf"); 30 | if (rc) { 31 | printf("init failed\n"); 32 | return -1; 33 | } 34 | 35 | zc = zlog_get_category("my_cat"); 36 | if (!zc) { 37 | printf("get cat fail\n"); 38 | zlog_fini(); 39 | return -2; 40 | } 41 | 42 | 43 | zlog_info(zc, "1.hello, zlog"); 44 | 45 | zlog_put_mdc("myname", "Zhang"); 46 | 47 | zlog_info(zc, "2.hello, zlog"); 48 | 49 | zlog_put_mdc("myname", "Li"); 50 | 51 | zlog_info(zc, "3.hello, zlog"); 52 | 53 | zlog_fini(); 54 | 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /test/test_mdc.conf: -------------------------------------------------------------------------------- 1 | [formats] 2 | mdc_format= "%d(%F %X.%ms) %-6V (%c:%F:%L) [%M(myname)] - %m%n" 3 | [rules] 4 | *.* >stdout; mdc_format 5 | -------------------------------------------------------------------------------- /test/test_multithread.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "zlog.h" 24 | 25 | #define CONFIG "test_multithread.conf" 26 | #define NB_THREADS 200 27 | #define THREAD_LOOP_DELAY 10000 /* 0.01" */ 28 | #define RELOAD_DELAY 10 29 | 30 | enum { 31 | ZLOG_LEVEL_TRACE = 10, 32 | ZLOG_LEVEL_SECURITY = 150, 33 | /* must equals conf file setting */ 34 | }; 35 | 36 | #define zlog_trace(cat, format, args...) \ 37 | zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \ 38 | ZLOG_LEVEL_TRACE, format, ##args) 39 | 40 | #define zlog_security(cat, format, args...) \ 41 | zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \ 42 | ZLOG_LEVEL_SECURITY, format, ##args) 43 | 44 | 45 | struct thread_info { /* Used as argument to thread_start() */ 46 | pthread_t thread_id; /* ID returned by pthread_create() */ 47 | int thread_num; /* Application-defined thread # */ 48 | zlog_category_t *zc; /* The logger category struc address; (All threads will use the same category, so he same address) */ 49 | long long int loop; /* Counter incremented to check the thread's health */ 50 | }; 51 | 52 | struct thread_info *tinfo; 53 | 54 | 55 | void intercept(int sig) 56 | { 57 | int i; 58 | 59 | printf("\nIntercept signal %d\n\n", sig); 60 | 61 | signal (sig, SIG_DFL); 62 | raise (sig); 63 | 64 | printf("You can import datas below in a spreadsheat and check if any thread stopped increment the Loop counter during the test.\n\n"); 65 | printf("Thread;Loop\n"); 66 | for (i=0; izc = zlog_get_category("thrd"); 81 | if (!tinfo->zc) { 82 | printf("get thrd %d cat fail\n", tinfo->thread_num); 83 | } 84 | else 85 | { 86 | while(1) 87 | { 88 | usleep(THREAD_LOOP_DELAY); 89 | zlog_info(tinfo->zc, "%d;%lld", tinfo->thread_num, tinfo->loop++); 90 | } 91 | } 92 | 93 | return NULL; 94 | } 95 | 96 | int main(int argc, char** argv) 97 | { 98 | int rc; 99 | zlog_category_t *zc; 100 | zlog_category_t *mc; 101 | zlog_category_t *hl; 102 | int i = 0; 103 | struct stat stat_0, stat_1; 104 | 105 | /* Create the logging directory if not yet ceated */ 106 | mkdir("./test_multithread-logs", 0777); 107 | 108 | if (stat(CONFIG, &stat_0)) 109 | { 110 | printf("Configuration file not found\n"); 111 | return -1; 112 | } 113 | 114 | rc = zlog_init(CONFIG); 115 | if (rc) { 116 | printf("main init failed\n"); 117 | return -2; 118 | } 119 | 120 | zc = zlog_get_category("main"); 121 | if (!zc) { 122 | printf("main get cat fail\n"); 123 | zlog_fini(); 124 | return -3; 125 | } 126 | 127 | mc = zlog_get_category("clsn"); 128 | if (!mc) { 129 | printf("clsn get cat fail\n"); 130 | zlog_fini(); 131 | return -3; 132 | } 133 | 134 | hl = zlog_get_category("high"); 135 | if (!hl) { 136 | printf("high get cat fail\n"); 137 | zlog_fini(); 138 | return -3; 139 | } 140 | 141 | /* Interrupt (ANSI). */ 142 | if (signal(SIGINT, intercept) == SIG_IGN ) 143 | { 144 | zlog_fatal(zc, "Can't caught the signal SIGINT, Interrupt (ANSI)"); 145 | signal(SIGINT, SIG_IGN ); 146 | return -4; 147 | } 148 | 149 | // start threads 150 | tinfo = calloc(NB_THREADS, sizeof(struct thread_info)); 151 | for (i=0; i 0) 199 | reload = (i % RELOAD_DELAY == 0); 200 | 201 | if (reload) 202 | { 203 | zlog_info(zc, "Will reload configuration..."); 204 | rc = zlog_reload(CONFIG); 205 | if (rc) { 206 | printf("main init failed\n"); 207 | return -6; 208 | } 209 | zlog_info(zc, "Configuration reloaded :)"); 210 | stat(CONFIG, &stat_0); 211 | } 212 | } 213 | 214 | exit(EXIT_SUCCESS); 215 | } 216 | -------------------------------------------------------------------------------- /test/test_multithread.conf: -------------------------------------------------------------------------------- 1 | [levels] 2 | TRACE = 10, LOG_DEBUG 3 | SECURITY = 150, LOG_ALERT 4 | [formats] 5 | simple = "%d(%m%d%H%M%S).%us %-6c %-10V [%06k] %m%n" 6 | security = "%d(%m%d%H%M%S).%us %-6c [%-8V] %m%n" 7 | developer = "%d(%m%d%H%M%S).%us %-6c %-10V [%t] %F(%L)/%U() - %m%n" 8 | csv = "%d.%ms;%m%n [%08t] [%T] [%06k] " 9 | [rules] 10 | clsn.ERROR >stdout; developer 11 | high.ERROR >stdout; security 12 | main.* >stdout; simple 13 | main.* "./test_multithread-logs.txt"; simple 14 | thrd.* "./test_multithread-logs/threadslog.csv", 20KB * 30 ~ "./test_multithread-logs/threadslog#r.csv"; csv 15 | -------------------------------------------------------------------------------- /test/test_pipe.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | zlog_category_t *zc; 23 | 24 | rc = zlog_init("test_pipe.conf"); 25 | if (rc) { 26 | printf("init failed\n"); 27 | return -1; 28 | } 29 | 30 | zc = zlog_get_category("my_cat"); 31 | if (!zc) { 32 | printf("get cat fail\n"); 33 | zlog_fini(); 34 | return -2; 35 | } 36 | 37 | zlog_info(zc, "hello, zlog"); 38 | zlog_fini(); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /test/test_pipe.conf: -------------------------------------------------------------------------------- 1 | [formats] 2 | simple = "%m%n" 3 | [rules] 4 | my_cat.* |/usr/bin/cronolog test_pipe_%Y%m%d.log; 5 | -------------------------------------------------------------------------------- /test/test_press_syslog.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | int work(long loop_count) 24 | { 25 | while(loop_count-- > 0) { 26 | syslog(LOG_INFO, "loglog"); 27 | } 28 | return 0; 29 | } 30 | 31 | 32 | int test(long process_count, long loop_count) 33 | { 34 | long i; 35 | pid_t pid; 36 | 37 | for (i = 0; i < process_count; i++) { 38 | pid = fork(); 39 | if (pid < 0) { 40 | printf("fork fail\n"); 41 | } else if(pid == 0) { 42 | work(loop_count); 43 | return 0; 44 | } 45 | } 46 | 47 | for (i = 0; i < process_count; i++) { 48 | pid = wait(NULL); 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | 55 | int main(int argc, char** argv) 56 | { 57 | 58 | if (argc != 3) { 59 | fprintf(stderr, "test nprocess nloop"); 60 | exit(1); 61 | } 62 | 63 | openlog(NULL, LOG_NDELAY|LOG_NOWAIT|LOG_PID, LOG_LOCAL0); 64 | 65 | test(atol(argv[1]), atol(argv[2])); 66 | 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /test/test_press_write.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | #include "zlog.h" 27 | 28 | int fd; 29 | static long loop_count; 30 | 31 | 32 | void * work(void *ptr) 33 | { 34 | long j = loop_count; 35 | static char aa[] = "2012-06-14 20:30:38.481187 INFO 24536:140716226213632:test_press_zlog.c:36 loglog\n"; 36 | 37 | while(j-- > 0) { 38 | // fprintf(fp, "2012-05-16 17:24:58.282603 INFO 22471:test_press_zlog.c:33 loglog\n"); 39 | // fwrite(aa, sizeof(aa)-1, 1, fp); 40 | write(fd, aa, sizeof(aa)-1); 41 | } 42 | return 0; 43 | } 44 | 45 | 46 | int test(long process_count, long thread_count) 47 | { 48 | long i; 49 | pid_t pid; 50 | long j; 51 | 52 | for (i = 0; i < process_count; i++) { 53 | pid = fork(); 54 | if (pid < 0) { 55 | printf("fork fail\n"); 56 | } else if(pid == 0) { 57 | pthread_t tid[thread_count]; 58 | 59 | for (j = 0; j < thread_count; j++) { 60 | pthread_create(&(tid[j]), NULL, work, NULL); 61 | } 62 | for (j = 0; j < thread_count; j++) { 63 | pthread_join(tid[j], NULL); 64 | } 65 | return 0; 66 | } 67 | } 68 | 69 | for (i = 0; i < process_count; i++) { 70 | pid = wait(NULL); 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | 77 | int main(int argc, char** argv) 78 | { 79 | if (argc != 4) { 80 | fprintf(stderr, "test nprocess nthreads nloop\n"); 81 | exit(1); 82 | } 83 | 84 | 85 | fd = open("press.log", O_CREAT | O_WRONLY | O_APPEND, 0644); 86 | //fp = fdopen(fd, "a"); 87 | loop_count = atol(argv[3]); 88 | test(atol(argv[1]), atol(argv[2])); 89 | //fclose(fp); 90 | close(fd); 91 | 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /test/test_press_write2.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "zlog.h" 26 | 27 | static long loop_count; 28 | 29 | 30 | void * work(void *ptr) 31 | { 32 | long j = loop_count; 33 | static char log[] = "2012-06-14 20:30:38.481187 INFO 24536:140716226213632:test_press_zlog.c:36 loglog\n"; 34 | char file[20]; 35 | 36 | sprintf(file, "press.%ld.log", (long)ptr); 37 | 38 | int fd; 39 | fd = open(file, O_CREAT | O_WRONLY | O_APPEND , 0644); 40 | //FILE *fp; 41 | 42 | while(j-- > 0) { 43 | write(fd, log, sizeof(log)-1); 44 | //fwrite(log, sizeof(log)-1, 1, fp); 45 | } 46 | //fclose(fp); 47 | close(fd); 48 | return 0; 49 | } 50 | 51 | 52 | int test(long process_count, long thread_count) 53 | { 54 | long i; 55 | pid_t pid; 56 | long j; 57 | 58 | for (i = 0; i < process_count; i++) { 59 | pid = fork(); 60 | if (pid < 0) { 61 | printf("fork fail\n"); 62 | } else if(pid == 0) { 63 | pthread_t tid[thread_count]; 64 | 65 | for (j = 0; j < thread_count; j++) { 66 | pthread_create(&(tid[j]), NULL, work, (void*)j); 67 | } 68 | for (j = 0; j < thread_count; j++) { 69 | pthread_join(tid[j], NULL); 70 | } 71 | return 0; 72 | } 73 | } 74 | 75 | for (i = 0; i < process_count; i++) { 76 | pid = wait(NULL); 77 | } 78 | 79 | return 0; 80 | } 81 | 82 | 83 | int main(int argc, char** argv) 84 | { 85 | if (argc != 4) { 86 | fprintf(stderr, "test nprocess nthreads nloop\n"); 87 | exit(1); 88 | } 89 | 90 | 91 | loop_count = atol(argv[3]); 92 | test(atol(argv[1]), atol(argv[2])); 93 | 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /test/test_press_zlog.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "zlog.h" 24 | 25 | static zlog_category_t *zc; 26 | static long loop_count; 27 | 28 | void * work(void *ptr) 29 | { 30 | long j = loop_count; 31 | while(j-- > 0) { 32 | zlog_info(zc, "loglog"); 33 | } 34 | return 0; 35 | } 36 | 37 | 38 | int test(long process_count, long thread_count) 39 | { 40 | long i; 41 | pid_t pid; 42 | long j; 43 | 44 | for (i = 0; i < process_count; i++) { 45 | pid = fork(); 46 | if (pid < 0) { 47 | printf("fork fail\n"); 48 | } else if(pid == 0) { 49 | pthread_t tid[thread_count]; 50 | for (j = 0; j < thread_count; j++) { 51 | pthread_create(&(tid[j]), NULL, work, NULL); 52 | } 53 | for (j = 0; j < thread_count; j++) { 54 | pthread_join(tid[j], NULL); 55 | } 56 | return 0; 57 | } 58 | } 59 | 60 | for (i = 0; i < process_count; i++) { 61 | pid = wait(NULL); 62 | } 63 | 64 | return 0; 65 | } 66 | 67 | 68 | int main(int argc, char** argv) 69 | { 70 | int rc; 71 | 72 | if (argc != 4) { 73 | fprintf(stderr, "test nprocess nthreads nloop\n"); 74 | exit(1); 75 | } 76 | 77 | rc = zlog_init("test_press_zlog.conf"); 78 | if (rc) { 79 | printf("init failed\n"); 80 | return 2; 81 | } 82 | 83 | zc = zlog_get_category("my_cat"); 84 | if (!zc) { 85 | printf("get cat failed\n"); 86 | zlog_fini(); 87 | return 3; 88 | } 89 | 90 | loop_count = atol(argv[3]); 91 | test(atol(argv[1]), atol(argv[2])); 92 | 93 | zlog_fini(); 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /test/test_press_zlog.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | #default format = "%d(%F %T).%us %-6V %p:%T:%F:%L %m%n" 3 | 4 | default format = "%d.%us %-6V %p:%T:%F:%L %m%n" 5 | 6 | [rules] 7 | # time ./test_press_zlog 1 10 100000 real user sys 8 | 9 | #*.* | /usr/bin/cronolog press%Y%m%d.log #1.632s 2.010s 1.100s 10 | *.* "press.log" #2.364s 2.090s 2.460s 11 | #*.* "press.log",10M #4.644s 2.540s 6.260s 12 | #*.* "press%d(%Y%m%d).log" #4.132s 2.910s 5.030s 13 | #*.* "press%d(%Y%m%d).log",1M*5 #4.713s 2.740s 6.310s 14 | #*.* "press.%d(%F).log",1MB ~ "press.#2r.log"#4.730s 2.690s 6.360s 15 | -------------------------------------------------------------------------------- /test/test_press_zlog2.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "zlog.h" 26 | 27 | static long loop_count; 28 | 29 | 30 | void * work(void *ptr) 31 | { 32 | long j = loop_count; 33 | char category[20]; 34 | sprintf(category, "cat%ld", (long)ptr); 35 | zlog_category_t *zc; 36 | 37 | zc = zlog_get_category(category); 38 | 39 | while(j-- > 0) { 40 | zlog_info(zc, "loglog"); 41 | } 42 | return 0; 43 | } 44 | 45 | 46 | int test(long process_count, long thread_count) 47 | { 48 | long i; 49 | pid_t pid; 50 | long j; 51 | 52 | for (i = 0; i < process_count; i++) { 53 | pid = fork(); 54 | if (pid < 0) { 55 | printf("fork fail\n"); 56 | } else if(pid == 0) { 57 | pthread_t tid[thread_count]; 58 | 59 | for (j = 0; j < thread_count; j++) { 60 | pthread_create(&(tid[j]), NULL, work, (void*)j); 61 | } 62 | for (j = 0; j < thread_count; j++) { 63 | pthread_join(tid[j], NULL); 64 | } 65 | return 0; 66 | } 67 | } 68 | 69 | for (i = 0; i < process_count; i++) { 70 | pid = wait(NULL); 71 | } 72 | 73 | return 0; 74 | } 75 | 76 | 77 | int main(int argc, char** argv) 78 | { 79 | int rc = 0; 80 | if (argc != 4) { 81 | fprintf(stderr, "test nprocess nthreads nloop\n"); 82 | exit(1); 83 | } 84 | 85 | rc = zlog_init("test_press_zlog2.conf"); 86 | if (rc) { 87 | printf("init failed\n"); 88 | return 2; 89 | } 90 | 91 | loop_count = atol(argv[3]); 92 | test(atol(argv[1]), atol(argv[2])); 93 | 94 | zlog_fini(); 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /test/test_press_zlog2.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | default format = "%d.%us %-6V %p:%T:%F:%L %m%n" 3 | 4 | [rules] 5 | cat0.* "press.0.log" 6 | cat1.* "press.1.log" 7 | cat2.* "press.2.log" 8 | cat3.* "press.3.log" 9 | cat4.* "press.4.log" 10 | cat5.* "press.5.log" 11 | cat6.* "press.6.log" 12 | cat7.* "press.7.log" 13 | cat8.* "press.8.log" 14 | cat9.* "press.9.log" 15 | -------------------------------------------------------------------------------- /test/test_profile.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | 23 | rc = dzlog_init("test_profile.conf", "my_cat"); 24 | if (rc) { 25 | printf("init failed\n"); 26 | return -1; 27 | } 28 | 29 | dzlog_info("hello, zlog"); 30 | 31 | zlog_profile(); 32 | 33 | zlog_fini(); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /test/test_profile.conf: -------------------------------------------------------------------------------- 1 | [formats] 2 | simple="%m%n" 3 | [rules] 4 | my_cat.* >stdout; simple 5 | -------------------------------------------------------------------------------- /test/test_prompt.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | #include 19 | 20 | int main(int argc, char** argv) 21 | { 22 | int rc; 23 | zlog_category_t *zc,*pzc; 24 | 25 | rc = zlog_init("test_prompt.conf"); 26 | if (rc) { 27 | printf("init failed\n"); 28 | return -1; 29 | } 30 | 31 | zc = zlog_get_category("my_cat"); 32 | pzc = zlog_get_category("prompt"); 33 | if (!zc || !pzc) { 34 | printf("get cat fail\n"); 35 | zlog_fini(); 36 | return -2; 37 | } 38 | 39 | zlog_debug(zc, "%s%d", "hello, zlog ", 1); 40 | zlog_info(zc, "hello, zlog 2"); 41 | 42 | for (int i =0; i<15;i++){ 43 | zlog_info(pzc, "prompt>"); 44 | sleep(1); 45 | if (! (i%3)) 46 | zlog_debug(zc, "dummy log entry %d",i); 47 | if (! (i%5)) 48 | zlog_info(zc, "hello, zlog %d",i); 49 | } 50 | zlog_info(zc, "done"); 51 | 52 | // zlog_profile(); 53 | 54 | zlog_fini(); 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /test/test_prompt.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | buffer min = 1024 4 | buffer max = 0 5 | 6 | [formats] 7 | simple = "%r%d %V %m%n" 8 | prompt = "%r%d(%T)>%m" 9 | 10 | [rules ] 11 | !.* >stdout;simple 12 | prompt.* >stdout;prompt 13 | -------------------------------------------------------------------------------- /test/test_record.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int output(zlog_msg_t *msg) 20 | { 21 | printf("[mystd]:[%s][%s][%ld]\n", msg->path, msg->buf, (long)msg->len); 22 | return 0; 23 | } 24 | 25 | int main(int argc, char** argv) 26 | { 27 | int rc; 28 | zlog_category_t *zc; 29 | 30 | rc = zlog_init("test_record.conf"); 31 | if (rc) { 32 | printf("init failed\n"); 33 | return -1; 34 | } 35 | 36 | zlog_set_record("myoutput", output); 37 | 38 | zc = zlog_get_category("my_cat"); 39 | if (!zc) { 40 | printf("get cat fail\n"); 41 | zlog_fini(); 42 | return -2; 43 | } 44 | 45 | zlog_info(zc, "hello, zlog"); 46 | zlog_fini(); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /test/test_record.conf: -------------------------------------------------------------------------------- 1 | [formats] 2 | simple = "%m%n" 3 | [rules] 4 | my_cat.* $myoutput, " mypath %c %d";simple 5 | -------------------------------------------------------------------------------- /test/test_syslog.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | 19 | int main(int argc, char** argv) 20 | { 21 | int rc; 22 | zlog_category_t *zc; 23 | 24 | rc = zlog_init("test_syslog.conf"); 25 | if (rc) { 26 | printf("init failed\n"); 27 | return -1; 28 | } 29 | 30 | zc = zlog_get_category("my_cat"); 31 | if (!zc) { 32 | printf("get cat fail\n"); 33 | zlog_fini(); 34 | return -2; 35 | } 36 | 37 | zlog_info(zc, "hello, zlog -- info"); 38 | zlog_error(zc, "hello, zlog -- error"); 39 | 40 | zlog_fini(); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /test/test_syslog.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | buffer min= 1024 4 | buffer max= 2MB 5 | rotate lock file = /tmp/zlog.lock 6 | default format = "default - %d(%F %X.%ms) %-6V (%c:%F:%U:%L) - %m%n" 7 | 8 | [formats] 9 | null = "%n" 10 | print = "print - [%-10.3d(%F)]%n" 11 | 12 | simple = "simple - %m%n" 13 | 14 | [rules] 15 | *.* >syslog , LOG_LOCAL0 16 | my_cat.* >stdout;simple 17 | -------------------------------------------------------------------------------- /test/test_tmp.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) Hardy Simpson 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "zlog.h" 18 | #include 19 | 20 | int main(int argc, char** argv) 21 | { 22 | int rc; 23 | zlog_category_t *zc; 24 | 25 | rc = zlog_init("test_tmp.conf"); 26 | if (rc) { 27 | printf("init failed\n"); 28 | return -1; 29 | } 30 | 31 | zc = zlog_get_category("my_cat"); 32 | if (!zc) { 33 | printf("get cat fail\n"); 34 | zlog_fini(); 35 | return -2; 36 | } 37 | 38 | zlog_debug(zc, "%s%d", "hello, zlog ", 1); 39 | zlog_info(zc, "hello, zlog 2"); 40 | 41 | sleep(1); 42 | 43 | zlog_info(zc, "hello, zlog 3"); 44 | zlog_debug(zc, "hello, zlog 4"); 45 | 46 | // zlog_profile(); 47 | 48 | zlog_fini(); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /test/test_tmp.conf: -------------------------------------------------------------------------------- 1 | [global] 2 | strict init = true 3 | buffer min = 1024 4 | buffer max = 0 5 | rotate lock file = /tmp/zlog.lock 6 | 7 | [formats] 8 | simple = "%d %V %m%n" 9 | simple2 = "%d %V %m%n" 10 | 11 | [rules ] 12 | *.=debug >stdout;simple 13 | *.=info >stdout;simple2 14 | -------------------------------------------------------------------------------- /test/val.sh: -------------------------------------------------------------------------------- 1 | unset ZLOG_PROFILE_ERROR 2 | unset ZLOG_PROFILE_DEBUG_LOG 3 | 4 | rm -f press*log 5 | 6 | valgrind --tool=callgrind ./test_press_zlog 1 10 10000 7 | -------------------------------------------------------------------------------- /tools/mk_targz.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$1" = "" ] 4 | then 5 | echo "Usage: mk_targz.sh " 6 | echo "Example: mk_targz.sh 1.2.7" 7 | exit 1 8 | fi 9 | 10 | if [ ! -d .git ] 11 | then 12 | echo "Must run at git home directory" 13 | exit 2 14 | fi 15 | 16 | HASH=`git show-ref --hash=8 refs/tags/${1}` 17 | PREFIX="zlog-${1}-${HASH}/" 18 | TARBALL="/tmp/zlog-${1}-${HASH}.tar.gz" 19 | git archive --format=tar -v --prefix=$PREFIX $1 | gzip -c > $TARBALL 20 | cp ${TARBALL} /tmp/zlog-latest-stable.tar.gz 21 | echo "File created: $TARBALL" 22 | -------------------------------------------------------------------------------- /zlog.spec: -------------------------------------------------------------------------------- 1 | Name: zlog 2 | Version: 1.2.8 3 | Release: 1 4 | Summary: zlog logger framework 5 | 6 | License: Apache License, Version 2.0 7 | URL: http://hardysimpson.github.io/zlog/ 8 | 9 | BuildRequires: gcc make 10 | 11 | %define _builddir %(echo $PWD) 12 | 13 | %description 14 | AIRTAME Web Service is powering the setup page. 15 | 16 | %build 17 | 18 | make clean all 19 | 20 | %install 21 | install -d '%{buildroot}/usr/include' 22 | install -d '%{buildroot}/usr/lib' 23 | cp src/zlog.h '%{buildroot}/usr/include/zlog.h' 24 | cp src/libzlog.a '%{buildroot}/usr/lib/libzlog.a' 25 | cp src/libzlog.so.1.2 '%{buildroot}/usr/lib/libzlog.so.1.2' 26 | ln -sf /usr/lib/libzlog.so.1.2 '%{buildroot}/usr/lib/libzlog.so.1' 27 | ln -sf /usr/lib/libzlog.so.1.2 '%{buildroot}/usr/lib/libzlog.so' 28 | 29 | %clean 30 | 31 | %files 32 | /usr/include/zlog.h 33 | /usr/lib/libzlog.a 34 | /usr/lib/libzlog.so 35 | /usr/lib/libzlog.so.1 36 | /usr/lib/libzlog.so.1.2 37 | 38 | 39 | --------------------------------------------------------------------------------