diff --git a/sample c++/log.cpp b/sample c++/log.cpp new file mode 100644 index 0000000..2e183b0 --- /dev/null +++ b/sample c++/log.cpp @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2017 rxi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include + +#include "log.h" + +static struct { + void *udata; + log_LockFn lock; + FILE *fp; + int level; + int quiet; +} L; + + +static const char *level_names[] = { + "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" +}; + +#ifdef LOG_USE_COLOR +static const char *level_colors[] = { + "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" +}; +#endif + + +static void lock(void) { + if (L.lock) { + L.lock(L.udata, 1); + } +} + + +static void unlock(void) { + if (L.lock) { + L.lock(L.udata, 0); + } +} + + +void log_set_udata(void *udata) { + L.udata = udata; +} + + +void log_set_lock(log_LockFn fn) { + L.lock = fn; +} + + +void log_set_fp(FILE *fp) { + L.fp = fp; +} + + +void log_set_level(int level) { + L.level = level; +} + + +void log_set_quiet(int enable) { + L.quiet = enable ? 1 : 0; +} + + +void log_log(int level, const char *file, int line, const char *fmt, ...) { + if (level < L.level) { + return; + } + + /* Acquire lock */ + lock(); + + /* Get current time */ + time_t t = time(NULL); + struct tm *lt = localtime(&t); + + /* Log to stderr */ + if (!L.quiet) { + va_list args; + char buf[16]; + buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0'; +#ifdef LOG_USE_COLOR + fprintf( + stderr, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", + buf, level_colors[level], level_names[level], file, line); +#else + fprintf(stderr, "%s %-5s %s:%d: ", buf, level_names[level], file, line); +#endif + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + fprintf(stderr, "\n"); + fflush(stderr); + } + + /* Log to file */ + if (L.fp) { + va_list args; + char buf[32]; + buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0'; + fprintf(L.fp, "%s %-5s %s:%d: ", buf, level_names[level], file, line); + va_start(args, fmt); + vfprintf(L.fp, fmt, args); + va_end(args); + fprintf(L.fp, "\n"); + fflush(L.fp); + } + + /* Release lock */ + unlock(); +} diff --git a/sample c++/log.h b/sample c++/log.h new file mode 100644 index 0000000..b3df494 --- /dev/null +++ b/sample c++/log.h @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2017 rxi + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See `log.c` for details. + */ + +#ifndef LOG_H +#define LOG_H + +#include +#include + +#define LOG_VERSION "0.1.0" + +typedef void (*log_LockFn)(void *udata, int lock); + +enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; + +#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) +#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) +#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) +#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__) +#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) +#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) + +void log_set_udata(void *udata); +void log_set_lock(log_LockFn fn); +void log_set_fp(FILE *fp); +void log_set_level(int level); +void log_set_quiet(int enable); + +void log_log(int level, const char *file, int line, const char *fmt, ...); + +#endif diff --git a/sample c++/sample/Debug/sample.exe b/sample c++/sample/Debug/sample.exe new file mode 100644 index 0000000..c71a941 Binary files /dev/null and b/sample c++/sample/Debug/sample.exe differ diff --git a/sample c++/sample/Debug/sample.ilk b/sample c++/sample/Debug/sample.ilk new file mode 100644 index 0000000..5ce9944 Binary files /dev/null and b/sample c++/sample/Debug/sample.ilk differ diff --git a/sample c++/sample/Debug/sample.pdb b/sample c++/sample/Debug/sample.pdb new file mode 100644 index 0000000..1d3964c Binary files /dev/null and b/sample c++/sample/Debug/sample.pdb differ diff --git a/sample c++/sample/ipch/sample-7fe17b0a/sample-c6634e.ipch b/sample c++/sample/ipch/sample-7fe17b0a/sample-c6634e.ipch new file mode 100644 index 0000000..134a9f6 Binary files /dev/null and b/sample c++/sample/ipch/sample-7fe17b0a/sample-c6634e.ipch differ diff --git a/sample c++/sample/sample.sdf b/sample c++/sample/sample.sdf new file mode 100644 index 0000000..a0d0fae Binary files /dev/null and b/sample c++/sample/sample.sdf differ diff --git a/sample c++/sample/sample.sln b/sample c++/sample/sample.sln new file mode 100644 index 0000000..473bded --- /dev/null +++ b/sample c++/sample/sample.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "sample", "sample\sample.vcxproj", "{2FA17684-60FD-4936-A07F-C7C551CB5521}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2FA17684-60FD-4936-A07F-C7C551CB5521}.Debug|Win32.ActiveCfg = Debug|Win32 + {2FA17684-60FD-4936-A07F-C7C551CB5521}.Debug|Win32.Build.0 = Debug|Win32 + {2FA17684-60FD-4936-A07F-C7C551CB5521}.Release|Win32.ActiveCfg = Release|Win32 + {2FA17684-60FD-4936-A07F-C7C551CB5521}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/sample c++/sample/sample.suo b/sample c++/sample/sample.suo new file mode 100644 index 0000000..e481daf Binary files /dev/null and b/sample c++/sample/sample.suo differ diff --git a/sample c++/sample/sample/Debug/CL.read.1.tlog b/sample c++/sample/sample/Debug/CL.read.1.tlog new file mode 100644 index 0000000..764b8b3 Binary files /dev/null and b/sample c++/sample/sample/Debug/CL.read.1.tlog differ diff --git a/sample c++/sample/sample/Debug/CL.write.1.tlog b/sample c++/sample/sample/Debug/CL.write.1.tlog new file mode 100644 index 0000000..1a24e0b Binary files /dev/null and b/sample c++/sample/sample/Debug/CL.write.1.tlog differ diff --git a/sample c++/sample/sample/Debug/cl.command.1.tlog b/sample c++/sample/sample/Debug/cl.command.1.tlog new file mode 100644 index 0000000..102b426 Binary files /dev/null and b/sample c++/sample/sample/Debug/cl.command.1.tlog differ diff --git a/sample c++/sample/sample/Debug/link.command.1.tlog b/sample c++/sample/sample/Debug/link.command.1.tlog new file mode 100644 index 0000000..49a012e Binary files /dev/null and b/sample c++/sample/sample/Debug/link.command.1.tlog differ diff --git a/sample c++/sample/sample/Debug/link.read.1.tlog b/sample c++/sample/sample/Debug/link.read.1.tlog new file mode 100644 index 0000000..524dc45 Binary files /dev/null and b/sample c++/sample/sample/Debug/link.read.1.tlog differ diff --git a/sample c++/sample/sample/Debug/link.write.1.tlog b/sample c++/sample/sample/Debug/link.write.1.tlog new file mode 100644 index 0000000..15323c6 Binary files /dev/null and b/sample c++/sample/sample/Debug/link.write.1.tlog differ diff --git a/sample c++/sample/sample/Debug/log.obj b/sample c++/sample/sample/Debug/log.obj new file mode 100644 index 0000000..02ceec6 Binary files /dev/null and b/sample c++/sample/sample/Debug/log.obj differ diff --git a/sample c++/sample/sample/Debug/mt.command.1.tlog b/sample c++/sample/sample/Debug/mt.command.1.tlog new file mode 100644 index 0000000..5dbc3c3 Binary files /dev/null and b/sample c++/sample/sample/Debug/mt.command.1.tlog differ diff --git a/sample c++/sample/sample/Debug/mt.read.1.tlog b/sample c++/sample/sample/Debug/mt.read.1.tlog new file mode 100644 index 0000000..e622c96 Binary files /dev/null and b/sample c++/sample/sample/Debug/mt.read.1.tlog differ diff --git a/sample c++/sample/sample/Debug/mt.write.1.tlog b/sample c++/sample/sample/Debug/mt.write.1.tlog new file mode 100644 index 0000000..882b5c5 Binary files /dev/null and b/sample c++/sample/sample/Debug/mt.write.1.tlog differ diff --git a/sample c++/sample/sample/Debug/sample.exe.intermediate.manifest b/sample c++/sample/sample/Debug/sample.exe.intermediate.manifest new file mode 100644 index 0000000..ecea6f7 --- /dev/null +++ b/sample c++/sample/sample/Debug/sample.exe.intermediate.manifest @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/sample c++/sample/sample/Debug/sample.lastbuildstate b/sample c++/sample/sample/Debug/sample.lastbuildstate new file mode 100644 index 0000000..7593a57 --- /dev/null +++ b/sample c++/sample/sample/Debug/sample.lastbuildstate @@ -0,0 +1,2 @@ +#v4.0:v100:false +Debug|Win32|C:\Work\NED\comunity\log.c\src\sample\| diff --git a/sample c++/sample/sample/Debug/sample.log b/sample c++/sample/sample/Debug/sample.log new file mode 100644 index 0000000..95e7a8c --- /dev/null +++ b/sample c++/sample/sample/Debug/sample.log @@ -0,0 +1,31 @@ +Build started 25-May-19 4:01:16 PM. + 1>Project "C:\Work\NED\comunity\log.c\src\sample\sample\sample.vcxproj" on node 2 (build target(s)). + 1>PrepareForBuild: + Creating directory "C:\Work\NED\comunity\log.c\src\sample\Debug\". + InitializeBuildStatus: + Creating "Debug\sample.unsuccessfulbuild" because "AlwaysCreate" was specified. + ClCompile: + C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc100.pdb" /Gd /TP /analyze- /errorReport:prompt ..\..\log.cpp sample.cpp + sample.cpp + 1>c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp(8): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. + c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen' + 1>c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp(16): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details. + c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch' + log.cpp + 1>c:\work\ned\comunity\log.c\src\log.cpp(100): warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. + c:\program files (x86)\microsoft visual studio 10.0\vc\include\time.inl(112) : see declaration of 'localtime' + Generating Code... + Link: + C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Work\NED\comunity\log.c\src\sample\Debug\sample.exe" /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /ManifestFile:"Debug\sample.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Work\NED\comunity\log.c\src\sample\Debug\sample.pdb" /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Work\NED\comunity\log.c\src\sample\Debug\sample.lib" /MACHINE:X86 Debug\log.obj + Debug\sample.obj + sample.vcxproj -> C:\Work\NED\comunity\log.c\src\sample\Debug\sample.exe + Manifest: + C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\mt.exe /nologo /verbose /outputresource:"C:\Work\NED\comunity\log.c\src\sample\Debug\sample.exe;#1" /manifest Debug\sample.exe.intermediate.manifest + FinalizeBuildStatus: + Deleting file "Debug\sample.unsuccessfulbuild". + Touching "Debug\sample.lastbuildstate". + 1>Done Building Project "C:\Work\NED\comunity\log.c\src\sample\sample\sample.vcxproj" (build target(s)). + +Build succeeded. + +Time Elapsed 00:00:02.06 diff --git a/sample c++/sample/sample/Debug/sample.obj b/sample c++/sample/sample/Debug/sample.obj new file mode 100644 index 0000000..623d868 Binary files /dev/null and b/sample c++/sample/sample/Debug/sample.obj differ diff --git a/sample c++/sample/sample/Debug/sample.write.1.tlog b/sample c++/sample/sample/Debug/sample.write.1.tlog new file mode 100644 index 0000000..e69de29 diff --git a/sample c++/sample/sample/Debug/vc100.idb b/sample c++/sample/sample/Debug/vc100.idb new file mode 100644 index 0000000..e13787c Binary files /dev/null and b/sample c++/sample/sample/Debug/vc100.idb differ diff --git a/sample c++/sample/sample/Debug/vc100.pdb b/sample c++/sample/sample/Debug/vc100.pdb new file mode 100644 index 0000000..cd0b0b2 Binary files /dev/null and b/sample c++/sample/sample/Debug/vc100.pdb differ diff --git a/sample c++/sample/sample/log.bin b/sample c++/sample/sample/log.bin new file mode 100644 index 0000000..7757dc0 --- /dev/null +++ b/sample c++/sample/sample/log.bin @@ -0,0 +1,6 @@ +2019-05-25 16:01:19 TRACE c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp:10: hello world +2019-05-25 16:01:19 DEBUG c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp:11: this is a debug message +2019-05-25 16:01:19 INFO c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp:12: passing information +2019-05-25 16:01:19 WARN c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp:13: this is a warning message +2019-05-25 16:01:19 ERROR c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp:14: this is an error message +2019-05-25 16:01:19 FATAL c:\work\ned\comunity\log.c\src\sample\sample\sample.cpp:15: this is a fatal message diff --git a/sample c++/sample/sample/sample.cpp b/sample c++/sample/sample/sample.cpp new file mode 100644 index 0000000..44ce8cc --- /dev/null +++ b/sample c++/sample/sample/sample.cpp @@ -0,0 +1,19 @@ +#include +#include +#include"..\..\log.h" + +void main(void) +{ + //if you want to save logs in a file + FILE *fp; + fp = fopen("log.bin","wb+"); + log_set_fp(fp); + //sample log messages + log_trace("hello %s", "world"); + log_debug("this is a debug message"); + log_info("passing information"); + log_warn("this is a warning message"); + log_error("this is an error message"); + log_fatal("this is a fatal message"); + getch(); +} \ No newline at end of file diff --git a/sample c++/sample/sample/sample.vcxproj b/sample c++/sample/sample/sample.vcxproj new file mode 100644 index 0000000..cc4fdcc --- /dev/null +++ b/sample c++/sample/sample/sample.vcxproj @@ -0,0 +1,72 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {2FA17684-60FD-4936-A07F-C7C551CB5521} + sample + + + + Application + true + MultiByte + + + Application + false + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + + + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/sample c++/sample/sample/sample.vcxproj.filters b/sample c++/sample/sample/sample.vcxproj.filters new file mode 100644 index 0000000..68695a3 --- /dev/null +++ b/sample c++/sample/sample/sample.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/sample c++/sample/sample/sample.vcxproj.user b/sample c++/sample/sample/sample.vcxproj.user new file mode 100644 index 0000000..ace9a86 --- /dev/null +++ b/sample c++/sample/sample/sample.vcxproj.user @@ -0,0 +1,3 @@ + + + \ No newline at end of file