Skip to content

Commit

Permalink
Print debug messages with timestamp and process ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kipper committed Jul 2, 2019
1 parent cfd62e6 commit c2721ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 2 additions & 3 deletions ext/semian/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,12 @@ semian_resource_reset_workers(VALUE self)
VALUE
semian_resource_unregister_worker(VALUE self)
{
int ret;
semian_resource_t *res = NULL;

TypedData_Get_Struct(self, semian_resource_t, &semian_resource_type, res);

sem_meta_lock(res->sem_id);
ret = perform_semop(res->sem_id, SI_SEM_REGISTERED_WORKERS, -1, IPC_NOWAIT | SEM_UNDO, NULL);
dprintf("Unregistering worker for sem_id:%d", res->sem_id);
int ret = perform_semop(res->sem_id, SI_SEM_REGISTERED_WORKERS, -1, IPC_NOWAIT | SEM_UNDO, NULL);
sem_meta_unlock(res->sem_id);

if ( ret == -1) {
Expand Down
7 changes: 6 additions & 1 deletion ext/semian/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdarg.h>
#include <stdio.h>
#include <time.h>

#include <openssl/sha.h>
#include <ruby.h>
Expand All @@ -16,7 +17,11 @@
#define dprintf(fmt, ...) \
do { \
if (DEBUG_TEST) { \
printf("[DEBUG] %s:%d - " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
const pid_t pid = getpid(); \
struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); \
struct tm t; localtime_r(&(ts.tv_sec), &t); \
char buf[128]; strftime(buf, sizeof(buf), "%H:%M:%S", &t); \
printf("%s.%ld [DEBUG] (%d): %s:%d - " fmt "\n", buf, ts.tv_nsec, pid, __FILE__, __LINE__, ##__VA_ARGS__); \
} \
} while (0)

Expand Down

0 comments on commit c2721ba

Please sign in to comment.