commit 48ce564130f8664f95d0a1efc3d5e57d81b08a21 Author: Bob Polis Date: Mon Mar 16 12:17:56 2020 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba71096 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +libsclogging diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..46d1ed8 --- /dev/null +++ b/Makefile @@ -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 diff --git a/log4cpp.cpp b/log4cpp.cpp new file mode 100644 index 0000000..5779285 --- /dev/null +++ b/log4cpp.cpp @@ -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 +#include +#include + +// C++ Standard Library +#include +#include +#include +#include +#include +#include +#include + +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 lock {m}; + print_timestamp(); + string f {file}; + va_list args1; + va_start(args1, fmt); + va_list args2; + va_copy(args2, args1); + vector 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; +} diff --git a/log4cpp.hpp b/log4cpp.hpp new file mode 100644 index 0000000..894e921 --- /dev/null +++ b/log4cpp.hpp @@ -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 +#include + +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__