Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample program for c++ added in the repository #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions sample c++/log.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>

#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();
}
35 changes: 35 additions & 0 deletions sample c++/log.h
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdarg.h>

#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
Binary file added sample c++/sample/Debug/sample.exe
Binary file not shown.
Binary file added sample c++/sample/Debug/sample.ilk
Binary file not shown.
Binary file added sample c++/sample/Debug/sample.pdb
Binary file not shown.
Binary file not shown.
Binary file added sample c++/sample/sample.sdf
Binary file not shown.
20 changes: 20 additions & 0 deletions sample c++/sample/sample.sln
Original file line number Diff line number Diff line change
@@ -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
Binary file added sample c++/sample/sample.suo
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/CL.read.1.tlog
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/CL.write.1.tlog
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/cl.command.1.tlog
Binary file not shown.
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/link.read.1.tlog
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/link.write.1.tlog
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/log.obj
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/mt.command.1.tlog
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/mt.read.1.tlog
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/mt.write.1.tlog
Binary file not shown.
10 changes: 10 additions & 0 deletions sample c++/sample/sample/Debug/sample.exe.intermediate.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level='asInvoker' uiAccess='false' />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
2 changes: 2 additions & 0 deletions sample c++/sample/sample/Debug/sample.lastbuildstate
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#v4.0:v100:false
Debug|Win32|C:\Work\NED\comunity\log.c\src\sample\|
31 changes: 31 additions & 0 deletions sample c++/sample/sample/Debug/sample.log
Original file line number Diff line number Diff line change
@@ -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
Binary file added sample c++/sample/sample/Debug/sample.obj
Binary file not shown.
Empty file.
Binary file added sample c++/sample/sample/Debug/vc100.idb
Binary file not shown.
Binary file added sample c++/sample/sample/Debug/vc100.pdb
Binary file not shown.
6 changes: 6 additions & 0 deletions sample c++/sample/sample/log.bin
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions sample c++/sample/sample/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<stdio.h>
#include<conio.h>
#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();
}
72 changes: 72 additions & 0 deletions sample c++/sample/sample/sample.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{2FA17684-60FD-4936-A07F-C7C551CB5521}</ProjectGuid>
<RootNamespace>sample</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\log.cpp" />
<ClCompile Include="sample.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\log.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
30 changes: 30 additions & 0 deletions sample c++/sample/sample/sample.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="sample.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\log.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\log.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions sample c++/sample/sample/sample.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>