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

windows support & shared fd in output function #55

Open
wants to merge 6 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
18 changes: 11 additions & 7 deletions doc/GettingStart-EN.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@

zlog is a reliable, high-performance, thread safe, flexible, clear-model, pure C logging library.

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.
So I wrote zlog.
It is faster, safer and more powerful than log4c. So it can be widely used.
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.
So I wrote zlog.
It is faster, safer and more powerful than log4c. So it can be widely used.

1. Install

Downloads: https://github.com/HardySimpson/zlog/releases

$ tar -zxvf zlog-latest-stable.tar.gz
$ cd zlog-latest-stable/
$ make
$ make
$ sudo make install
or
$ make PREFIX=/usr/local/
$ sudo make PREFIX=/usr/local/ install

PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure your program can find zlog library.
PREFIX indicates the installation destination for zlog. After installation, refresh your dynamic linker to make sure your program can find zlog library.

$ sudo vi /etc/ld.so.conf
/usr/local/lib
$ sudo ldconfig

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.

On Windows, you need to download the following dependencies:
pthread 2.9.1: https://sourceware.org/pthreads-win32/
unixem 1.9.1: http://synesis.com.au/software/unixem.html
syslog win32: https://github.com/fbzhong/syslog-win32

2. Introduce configure file

Expand All @@ -52,7 +56,7 @@ my_cat.DEBUG "/var/log/aa.log", 1M; simple
3. Using zlog API in C source file
$ vi test_hello.c

#include <stdio.h>
#include <stdio.h>

#include "zlog.h"

Expand All @@ -79,7 +83,7 @@ int main(int argc, char** argv)
zlog_fini();

return 0;
}
}

4. Complie, and run it!s

Expand Down
10 changes: 9 additions & 1 deletion src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@
#define ZLOG_CONF_DEFAULT_FILE_PERMS 0600
#define ZLOG_CONF_DEFAULT_RELOAD_CONF_PERIOD 0
#define ZLOG_CONF_DEFAULT_FSYNC_PERIOD 0

#ifdef _MSC_VER
#define DEF_TIME_FMT "%Y-%m-%d %H:%M:%S"
#define ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE "zlog.lock"
#else
#define DEF_TIME_FMT "%F %T"
#define ZLOG_CONF_BACKUP_ROTATE_LOCK_FILE "/tmp/zlog.lock"
#endif

/*******************************************************************************/

void zlog_conf_profile(zlog_conf_t * a_conf, int flag)
Expand Down Expand Up @@ -239,7 +247,7 @@ static int zlog_conf_build_with_file(zlog_conf_t * a_conf)
return -1;
}
localtime_r(&(a_stat.st_mtime), &local_time);
strftime(a_conf->mtime, sizeof(a_conf->mtime), "%F %T", &local_time);
strftime(a_conf->mtime, sizeof(a_conf->mtime), DEF_TIME_FMT, &local_time);

if ((fp = fopen(a_conf->file, "r")) == NULL) {
zc_error("open configure file[%s] fail", a_conf->file);
Expand Down
12 changes: 9 additions & 3 deletions src/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

#include <pthread.h>
#include <unistd.h>
#ifndef _MSC_VER
#include <sys/time.h>
#endif

#include "zc_defs.h"
#include "event.h"
Expand All @@ -31,7 +33,7 @@ void zlog_event_profile(zlog_event_t * a_event, int flag)
a_event->line, a_event->level,
a_event->hex_buf, a_event->str_format,
a_event->time_stamp.tv_sec, a_event->time_stamp.tv_usec,
(long)a_event->pid, (long)a_event->tid,
(long)a_event->pid, (long)tidname(a_event),
a_event->time_cache_count);
return;
}
Expand Down Expand Up @@ -70,7 +72,11 @@ zlog_event_t *zlog_event_new(int time_cache_count)
* u don't always change your hostname, eh?
*/
if (gethostname(a_event->host_name, sizeof(a_event->host_name) - 1)) {
#ifdef _MSC_VER
zc_error("gethostname fail, errno[%d]", WSAGetLastError());
#else
zc_error("gethostname fail, errno[%d]", errno);
#endif
goto err;
}

Expand All @@ -82,8 +88,8 @@ zlog_event_t *zlog_event_new(int time_cache_count)
*/
a_event->tid = pthread_self();

a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)a_event->tid);
a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "0x%lu", (unsigned long)a_event->tid);
a_event->tid_str_len = sprintf(a_event->tid_str, "%lu", (unsigned long)tidname(a_event));
a_event->tid_hex_str_len = sprintf(a_event->tid_hex_str, "0x%x", (unsigned int)tidname(a_event));

//zlog_event_profile(a_event, ZC_DEBUG);
return a_event;
Expand Down
4 changes: 4 additions & 0 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ ifeq ($(uname_S),Darwin)
DYLIBSUFFIX=dylib
DYLIB_MINOR_NAME=$(LIBNAME).$(ZLOG_MAJOR).$(ZLOG_MINOR).$(DYLIBSUFFIX)
DYLIB_MAJOR_NAME=$(LIBNAME).$(ZLOG_MAJOR).$(DYLIBSUFFIX)
<<<<<<< Updated upstream
DYLIB_MAKE_CMD=$(CC) -dynamiclib -install_name $(INSTALL_LIBRARY_PATH)/$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS)
=======
DYLIB_MAKE_CMD=$(CC) -shared -Wl,-install_name,$(DYLIB_MINOR_NAME) -o $(DYLIBNAME) $(LDFLAGS)
>>>>>>> Stashed changes
endif

ifeq ($(uname_S),AIX)
Expand Down
41 changes: 40 additions & 1 deletion src/rotater.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
*/

#ifdef _MSC_VER
#include <io.h>
#include <windows.h>
#include <unixem/glob.h>
#endif

#include <string.h>
#include <glob.h>
#include <stdio.h>
Expand Down Expand Up @@ -64,9 +70,15 @@ void zlog_rotater_del(zlog_rotater_t *a_rotater)
zc_assert(a_rotater,);

if (a_rotater->lock_fd) {
#ifdef _MSC_VER
if (CloseHandle(a_rotater->lock_fd)) {
zc_error("close fail, errno[%d]", errno);
}
#else
if (close(a_rotater->lock_fd)) {
zc_error("close fail, errno[%d]", errno);
}
#endif
}

if (pthread_mutex_destroy(&(a_rotater->lock_mutex))) {
Expand All @@ -80,7 +92,7 @@ void zlog_rotater_del(zlog_rotater_t *a_rotater)

zlog_rotater_t *zlog_rotater_new(char *lock_file)
{
int fd = 0;
zlogfd fd = 0;
zlog_rotater_t *a_rotater;

zc_assert(lock_file, NULL);
Expand All @@ -102,12 +114,23 @@ zlog_rotater_t *zlog_rotater_new(char *lock_file)
* user B is unable to read /tmp/zlog.lock
* B has to choose another lock file except /tmp/zlog.lock
*/
#ifdef _MSC_VER
fd = CreateFile(lock_file,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(fd == INVALID_HANDLE_VALUE) {
zc_error("open file[%s] fail, errno[%d]", lock_file, GetLastError());
goto err;
}
#else
fd = open(lock_file, O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd < 0) {
zc_error("open file[%s] fail, errno[%d]", lock_file, errno);
goto err;
}
#endif

a_rotater->lock_fd = fd;
a_rotater->lock_file = lock_file;
Expand Down Expand Up @@ -469,12 +492,14 @@ static int zlog_rotater_lsmv(zlog_rotater_t *a_rotater,
static int zlog_rotater_trylock(zlog_rotater_t *a_rotater)
{
int rc;
#ifndef _MSC_VER
struct flock fl;

fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
#endif

rc = pthread_mutex_trylock(&(a_rotater->lock_mutex));
if (rc == EBUSY) {
Expand All @@ -485,6 +510,12 @@ static int zlog_rotater_trylock(zlog_rotater_t *a_rotater)
return -1;
}

#ifdef _MSC_VER
if (LockFile(a_rotater->lock_fd,999,0,1,0)==0) {
zc_error("lock fd[%d] fail", a_rotater->lock_fd);
return(-1);
}
#else
if (fcntl(a_rotater->lock_fd, F_SETLK, &fl)) {
if (errno == EAGAIN || errno == EACCES) {
/* lock by other process, that's right, go on */
Expand All @@ -499,13 +530,15 @@ static int zlog_rotater_trylock(zlog_rotater_t *a_rotater)
}
return -1;
}
#endif

return 0;
}

static int zlog_rotater_unlock(zlog_rotater_t *a_rotater)
{
int rc = 0;
#ifndef _MSC_VER
struct flock fl;

fl.l_type = F_UNLCK;
Expand All @@ -517,6 +550,12 @@ static int zlog_rotater_unlock(zlog_rotater_t *a_rotater)
rc = -1;
zc_error("unlock fd[%s] fail, errno[%d]", a_rotater->lock_fd, errno);
}
#else
if (UnlockFile(a_rotater->lock_fd,999,0,1,0)==0) {
rc = -1;
zc_error("unlock fd[%s] fail", a_rotater->lock_fd);
}
#endif

if (pthread_mutex_unlock(&(a_rotater->lock_mutex))) {
rc = -1;
Expand Down
4 changes: 4 additions & 0 deletions src/rotater.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
typedef struct zlog_rotater_s {
pthread_mutex_t lock_mutex;
char *lock_file;
#ifdef _MSC_VER
HANDLE lock_fd;
#else
int lock_fd;
#endif

/* single-use members */
char *base_path; /* aa.log */
Expand Down
Loading