first commit

This commit is contained in:
Bob Polis 2020-03-16 12:17:56 +01:00
commit 48ce564130
4 changed files with 179 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
libsclogging

80
Makefile Normal file
View File

@ -0,0 +1,80 @@
LIBNAME := $(shell basename $$(pwd))
MAJOR := 1
MINOR := 0.0
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
LINKERNAME := $(LIBNAME).dylib
SONAME := $(LIBNAME).$(MAJOR).dylib
REALNAME := $(LINKERNAME)
else
LINKERNAME := $(LIBNAME).so
SONAME := $(LINKERNAME).$(MAJOR)
REALNAME := $(SONAME).$(MINOR)
endif
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
LIBDIR ?= $(PREFIX)/lib
INCLUDEDIR ?= $(PREFIX)/include
DATADIR ?= $(PREFIX)/share
MANDIR ?= $(DATADIR)/man
SRCS := $(wildcard *.cpp)
OBJS := $(subst .cpp,.o,$(SRCS))
DEPS := $(subst .cpp,.d,$(SRCS))
HDRS := $(filter-out $(LIBNAME).hpp,$(wildcard *.hpp))
CXX ?= g++
CXXFLAGS := $(CXXFLAGS) -Wshadow -Wall -Wpedantic -Wextra -g -fno-strict-aliasing -std=c++17 -fPIC
ifeq ($(DEBUG),1)
CXXFLAGS += -D DEBUG -O0
else
CXXFLAGS += -D NDEBUG -O3
endif
LDLIBS :=
RM := /bin/rm -f
INSTALL := /usr/bin/install -c
all: $(REALNAME)
$(REALNAME): $(OBJS) $(DEPS)
ifeq ($(UNAME_S),Darwin)
$(CXX) -dynamiclib -o $(REALNAME) -current_version $(MAJOR) -compatibility_version $(MINOR) $(LDFLAGS) $(LDLIBS) $(OBJS)
else
$(CXX) -g -shared -Wl,-soname,$(SONAME) -o $(REALNAME) $(LDFLAGS) $(LDLIBS) $(OBJS)
endif
%.o: %.cpp %.d Makefile
$(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MF $*.d -c $<
-include *.d
%.d: ;
$(LIBNAME).hpp: $(HDRS)
@echo updating $(LIBNAME).hpp
@cp /dev/null $(LIBNAME).hpp
@for h in $(HDRS); \
do \
cat $$h >> $(LIBNAME).hpp; \
done
.PHONY: clean install
clean:
$(RM) $(OBJS) $(DEPS) $(REALNAME) $(LIBNAME).hpp
install: $(REALNAME) $(LIBNAME).hpp
$(INSTALL) -m 644 $(REALNAME) $(LIBDIR)
$(INSTALL) -m 644 $(LIBNAME).hpp $(INCLUDEDIR)
ifeq ($(UNAME_S),Darwin)
cd $(LIBDIR) && ln -sf $(REALNAME) $(SONAME)
else
ldconfig
cd $(LIBDIR) && ln -sf $(SONAME) $(LINKERNAME)
endif

68
log4cpp.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
* log4cpp.cpp
*
* Created by Bob Polis on 05-07-2007.
* Copyright 2007 Thalictrum. All rights reserved.
*/
#include "log4cpp.hpp"
// UNIX
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
// C++ Standard Library
#include <cstdio>
#include <cstdarg>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#include <mutex>
using namespace std;
stringstream glog;
pthread_mutex_t log4cpp_lock = PTHREAD_MUTEX_INITIALIZER;
mutex m;
void print_timestamp()
{
struct timeval secs;
(void)gettimeofday(&secs, NULL);
time_t now = time(NULL);
char dtstamp[32];
strftime(dtstamp, 32, "%F %T", localtime(&now));
long millisecs = (long)((double)secs.tv_usec / 1000.0 + 0.5) % 1000;
#if __APPLE__
fprintf(stderr, "%s.%03ld %s[%ld] ", dtstamp, millisecs, getprogname(), (long)getpid());
#elif __linux__
fprintf(stderr, "%s.%03ld %s[%ld] ", dtstamp, millisecs, program_invocation_name, (long)getpid());
#endif
}
void __slog(const char* func, const char* file, int line, stringstream& ss)
{
::pthread_mutex_lock(&log4cpp_lock);
print_timestamp();
string f(file);
cerr << "(" << f.substr(f.rfind('/') + 1) << ":" << line << ") " << func << "(): " << ss.str() << endl;
ss.str("");
::pthread_mutex_unlock(&log4cpp_lock);
}
void _dlog(const char* func, const char* file, int line, const char* fmt, ...) {
lock_guard<mutex> lock {m};
print_timestamp();
string f {file};
va_list args1;
va_start(args1, fmt);
va_list args2;
va_copy(args2, args1);
vector<char> buf(1 + vsnprintf(nullptr, 0, fmt, args1));
va_end(args1);
vsnprintf(buf.data(), buf.size(), fmt, args2);
va_end(args2);
cerr << "(" << f.substr(f.rfind('/') + 1) << ":" << line << ") " << func << "(): " << buf.data() << endl;
}

30
log4cpp.hpp Normal file
View File

@ -0,0 +1,30 @@
/*
* log4cpp.h
*
* Created by Bob Polis on 05-07-2007.
* Copyright 2007 Thalictrum. All rights reserved.
*/
#ifndef __log4cpp__
#define __log4cpp__
#include <sstream>
#include <pthread.h>
extern std::stringstream glog; // WARNING: *not* thread-safe!
void print_timestamp();
void __slog(const char* func, const char* file, int line, std::stringstream& ss);
void _dlog(const char* func, const char* file, int line, const char* fmt, ...);
#if DEBUG
#define _slog(ss) __slog(__FUNCTION__, __FILE__, __LINE__, (std::stringstream&)(ss));
#define dlog(fmt, ...) _dlog(__FUNCTION__, __FILE__, __LINE__, fmt, __VA_ARGS__);
#else
#define _slog(ss)
#define dlog(fmt, ...)
#endif
#define sclog(fmt, ...) _dlog(__FUNCTION__, __FILE__, __LINE__, fmt, __VA_ARGS__);
#endif //__log4cpp__