pdbg split from plibs

This commit is contained in:
Levi Neely 2025-03-13 19:58:22 +01:00
parent 9da561c767
commit 53c926893a
9 changed files with 274 additions and 275 deletions

View File

@ -36,6 +36,7 @@
#include "psettings.h"
#include "psynclib.h"
#include "ptimer.h"
#include "pdbg.h"
/*
commented definitions are unused, but kept because they may be

72
pclsync/pdbg.c Normal file
View File

@ -0,0 +1,72 @@
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "pdbg.h"
#include "ppath.h"
#include "putil.h"
extern PSYNC_THREAD const char *psync_thread_name;
char *psync_debug_path() {
char *home = ppath_home();
if (!home) {
return NULL;
}
const char *subdir = "/.pcloud/debug.log";
size_t len = strlen(home) + strlen(subdir) + 1;
char *sockpath = (char *)malloc(len);
if (!sockpath) {
return NULL;
}
snprintf(sockpath, len, "%s%s", home, subdir);
return sockpath;
}
int psync_debug(const char *file, const char *function, int unsigned line, int unsigned level, const char *fmt, ...) {
if (!IS_DEBUG)
return 1;
static const struct {
unsigned long level;
const char *name;
} debug_levels[] = DEBUG_LEVELS;
static FILE *log = NULL;
struct timespec ts;
char dttime[36], format[512];
va_list ap;
const char *errname;
unsigned long i;
unsigned int u;
pthread_t threadid;
errname = "BAD_ERROR_CODE";
for (i = 0; i < ARRAY_SIZE(debug_levels); i++)
if (debug_levels[i].level == level) {
errname = debug_levels[i].name;
break;
}
if (unlikely(!log)) {
char *path = psync_debug_path();
log = fopen(path, "a+");
free(path);
if (!log)
return 1;
}
clock_gettime(CLOCK_REALTIME, &ts);
time_format(ts.tv_sec, ts.tv_nsec, dttime);
threadid = pthread_self();
memcpy(&u, &threadid, sizeof(u));
snprintf(format, sizeof(format), "%s %u %s %s: %s:%u (function %s): %s\n",
dttime, u, psync_thread_name, errname, file, line, function, fmt);
format[sizeof(format) - 1] = 0;
va_start(ap, fmt);
vfprintf(log, format, ap);
va_end(ap);
fflush(log);
return 1;
}

100
pclsync/pdbg.h Normal file
View File

@ -0,0 +1,100 @@
#ifndef __PDBG_H
#define __PDBG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "pcompiler.h"
#include "pstatus.h"
#include "psynclib.h" // need for macros...
#define D_NONE 0
#define D_BUG 10
#define D_CRITICAL 20
#define D_ERROR 30
#define D_WARNING 40
#define D_NOTICE 50
#define DEBUG_LEVELS {{D_BUG, "BUG"}, {D_CRITICAL, "CRITICAL ERROR"}, {D_ERROR, "ERROR"}, {D_WARNING, "WARNING"}, {D_NOTICE, "NOTICE" } }
#ifndef DEBUG_LEVEL
#define DEBUG_LEVEL D_NOTICE
#endif
#define IS_DEBUG (DEBUG_LEVEL >= D_WARNING)
#if defined(assert)
#undef assert
#endif
#define debug(level, ...) \
do { \
if (level <= DEBUG_LEVEL) \
psync_debug(__FILE__, __FUNCTION__, __LINE__, level, __VA_ARGS__); \
} while (0)
#define assert(cond) \
do { \
if (D_WARNING <= DEBUG_LEVEL && unlikely(!(cond))) { \
debug(D_WARNING, "assertion %s failed, aborting", TO_STR(cond)); \
abort(); \
} \
} while (0)
#define assertw(cond) \
do { \
if (D_WARNING <= DEBUG_LEVEL && unlikely(!(cond))) { \
debug(D_WARNING, "assertion %s failed", TO_STR(cond)); \
} \
} while (0)
#define debug_execute(level, expr) \
do { \
if (level <= DEBUG_LEVEL) \
(expr); \
} while (0)
#if IS_DEBUG
#define likely_log(x) \
(likely(x) ? 1 \
: psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"assertion likely_log(%s) failed", TO_STR(x)) * \
0)
#define unlikely_log(x) \
(unlikely(x) ? psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"assertion unlikely_log(%s) failed", TO_STR(x)) \
: 0)
#define PRINT_RETURN(x) \
((x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_NOTICE, \
"returning %d", (int)(x)))
#define PRINT_RETURN_CONST(x) \
((x) * \
psync_debug(__FILE__, __FUNCTION__, __LINE__, D_NOTICE, "returning " #x))
#define PRINT_RETURN_FORMAT(x, format, ...) \
((x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_NOTICE, \
"returning %d" format, (int)(x), __VA_ARGS__))
#define PRINT_NEG_RETURN(x) \
((x < 0) ? (x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"returning %d", (int)(x)) \
: (x))
#define PRINT_NEG_RETURN_FORMAT(x, format, ...) \
((x < 0) ? (x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"returning %d " format, (int)(x), __VA_ARGS__) \
: (x))
#else
#define likely_log likely
#define unlikely_log unlikely
#define PRINT_RETURN(x) (x)
#define PRINT_RETURN_CONST(x) (x)
#define PRINT_RETURN_FORMAT(x, format, ...) (x)
#define PRINT_NEG_RETURN(x) (x)
#define PRINT_NEG_RETURN_FORMAT(x, format, ...) (x)
#endif
int psync_debug(const char *file, const char *function, int unsigned line, int unsigned level, const char *fmt, ...) PSYNC_COLD PSYNC_FORMAT(printf, 5, 6) PSYNC_NONNULL(5);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -29,24 +29,25 @@
DAMAGE.
*/
#include "plibs.h"
#include "pcache.h"
#include "pdatabase.h"
#include "plocks.h"
#include "pnetlibs.h"
#include "ppath.h"
#include "prun.h"
#include "psettings.h"
#include "ptimer.h"
#include "psys.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "pfile.h"
// required by psync_debug
#include "pcache.h"
#include "pdatabase.h"
#include "pdbg.h"
#include "pfile.h"
#include "plibs.h"
#include "plocks.h"
#include "pnetlibs.h"
#include "prun.h"
#include "psettings.h"
#include "psys.h"
#include "ptimer.h"
#include "putil.h"
extern PSYNC_THREAD const char *psync_thread_name;
#define return_error(err) \
@ -613,7 +614,6 @@ static rd_lock_data *record_rdunlock() {
return lock;
}
static void time_format(time_t tm, unsigned long ns, char *result);
void psync_sql_dump_locks() {
rd_lock_data *lock;
@ -2529,111 +2529,6 @@ void psync_try_free_memory() {
pcache_clean();
}
static void time_format(time_t tm, unsigned long ns, char *result) {
static const char month_names[12][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
static const char day_names[7][4] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"};
struct tm dt;
unsigned long y;
ns /= 1000000;
gmtime_r(&tm, &dt);
memcpy(result, day_names[dt.tm_wday], 3);
result += 3;
*result++ = ',';
*result++ = ' ';
*result++ = dt.tm_mday / 10 + '0';
*result++ = dt.tm_mday % 10 + '0';
*result++ = ' ';
memcpy(result, month_names[dt.tm_mon], 3);
result += 3;
*result++ = ' ';
y = dt.tm_year + 1900;
*result++ = '0' + y / 1000;
y = y % 1000;
*result++ = '0' + y / 100;
y = y % 100;
*result++ = '0' + y / 10;
y = y % 10;
*result++ = '0' + y;
*result++ = ' ';
*result++ = dt.tm_hour / 10 + '0';
*result++ = dt.tm_hour % 10 + '0';
*result++ = ':';
*result++ = dt.tm_min / 10 + '0';
*result++ = dt.tm_min % 10 + '0';
*result++ = ':';
*result++ = dt.tm_sec / 10 + '0';
*result++ = dt.tm_sec % 10 + '0';
*result++ = '.';
*result++ = ns / 100 + '0';
*result++ = (ns / 10) % 10 + '0';
*result++ = ns % 10 + '0';
memcpy(result, " +0000", 7); // copies the null byte
}
char *psync_debug_path() {
char *home = ppath_home();
if (!home) {
return NULL;
}
const char *subdir = "/.pcloud/debug.log";
size_t len = strlen(home) + strlen(subdir) + 1;
char *sockpath = (char *)malloc(len);
if (!sockpath) {
return NULL;
}
snprintf(sockpath, len, "%s%s", home, subdir);
return sockpath;
}
int psync_debug(const char *file, const char *function, int unsigned line,
int unsigned level, const char *fmt, ...) {
if (!IS_DEBUG)
return 1;
static const struct {
unsigned long level;
const char *name;
} debug_levels[] = DEBUG_LEVELS;
static FILE *log = NULL;
struct timespec ts;
char dttime[36], format[512];
va_list ap;
const char *errname;
unsigned long i;
unsigned int u;
pthread_t threadid;
errname = "BAD_ERROR_CODE";
for (i = 0; i < ARRAY_SIZE(debug_levels); i++)
if (debug_levels[i].level == level) {
errname = debug_levels[i].name;
break;
}
if (unlikely(!log)) {
char *path = psync_debug_path();
log = fopen(path, "a+");
free(path);
if (!log)
return 1;
}
clock_gettime(CLOCK_REALTIME, &ts);
time_format(ts.tv_sec, ts.tv_nsec, dttime);
threadid = pthread_self();
memcpy(&u, &threadid, sizeof(u));
snprintf(format, sizeof(format), "%s %u %s %s: %s:%u (function %s): %s\n",
dttime, u, psync_thread_name, errname, file, line, function, fmt);
format[sizeof(format) - 1] = 0;
va_start(ap, fmt);
vfprintf(log, format, ap);
va_end(ap);
fflush(log);
return 1;
}
static const char *PSYNC_CONST get_type_name(uint32_t t) {
if (unlikely(t >= ARRAY_SIZE(psync_typenames)))
t = 0;

View File

@ -32,6 +32,7 @@
#ifndef _PSYNC_LIBS_H
#define _PSYNC_LIBS_H
#include "putil.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -40,69 +41,16 @@ extern "C" {
#include "pcompiler.h"
#include "pstatus.h"
#include "putil.h"
#include "pdbg.h"
#include "psynclib.h" // need for macros...
#include <sqlite3.h>
#include <stdint.h>
#include <string.h>
#define D_NONE 0
#define D_BUG 10
#define D_CRITICAL 20
#define D_ERROR 30
#define D_WARNING 40
#define D_NOTICE 50
// required for pcloud api; 7=>linux
#define P_OS_ID 7
#define DEBUG_LEVELS \
{ \
{D_BUG, "BUG"}, {D_CRITICAL, "CRITICAL ERROR"}, {D_ERROR, "ERROR"}, \
{D_WARNING, "WARNING"}, { \
D_NOTICE, "NOTICE" \
} \
}
#ifndef DEBUG_LEVEL
#define DEBUG_LEVEL D_NOTICE
#endif
#define IS_DEBUG (DEBUG_LEVEL >= D_WARNING)
#if defined(assert)
#undef assert
#endif
#define PSYNC_SSL_DEBUG_LEVEL \
0 /* Please make sure this setting is always set to 0 for release builds !!! \
Possible values are in the range [0, 5] */
#define debug(level, ...) \
do { \
if (level <= DEBUG_LEVEL) \
psync_debug(__FILE__, __FUNCTION__, __LINE__, level, __VA_ARGS__); \
} while (0)
#define assert(cond) \
do { \
if (D_WARNING <= DEBUG_LEVEL && unlikely(!(cond))) { \
debug(D_WARNING, "assertion %s failed, aborting", TO_STR(cond)); \
abort(); \
} \
} while (0)
#define assertw(cond) \
do { \
if (D_WARNING <= DEBUG_LEVEL && unlikely(!(cond))) { \
debug(D_WARNING, "assertion %s failed", TO_STR(cond)); \
} \
} while (0)
#define debug_execute(level, expr) \
do { \
if (level <= DEBUG_LEVEL) \
(expr); \
} while (0)
#define PSYNC_TNUMBER 1
#define PSYNC_TSTRING 2
#define PSYNC_TREAL 3
@ -161,15 +109,6 @@ extern "C" {
: psync_err_real_expected(__FILE__, __FUNCTION__, __LINE__, &(v)))
#if D_WARNING <= DEBUG_LEVEL
#define likely_log(x) \
(likely(x) ? 1 \
: psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"assertion likely_log(%s) failed", TO_STR(x)) * \
0)
#define unlikely_log(x) \
(unlikely(x) ? psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"assertion unlikely_log(%s) failed", TO_STR(x)) \
: 0)
#if defined(PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP)
#undef PTHREAD_MUTEX_INITIALIZER
#define PTHREAD_MUTEX_INITIALIZER PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
@ -190,35 +129,8 @@ extern "C" {
abort(); \
} \
} while (0)
#define PRINT_RETURN(x) \
((x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_NOTICE, \
"returning %d", (int)(x)))
#define PRINT_RETURN_CONST(x) \
((x) * \
psync_debug(__FILE__, __FUNCTION__, __LINE__, D_NOTICE, "returning " #x))
#define PRINT_RETURN_FORMAT(x, format, ...) \
((x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_NOTICE, \
"returning %d" format, (int)(x), __VA_ARGS__))
#define PRINT_NEG_RETURN(x) \
((x < 0) ? (x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"returning %d", (int)(x)) \
: (x))
#define PRINT_NEG_RETURN_FORMAT(x, format, ...) \
((x < 0) ? (x) * psync_debug(__FILE__, __FUNCTION__, __LINE__, D_WARNING, \
"returning %d " format, (int)(x), __VA_ARGS__) \
: (x))
#else
#define likely_log likely
#define unlikely_log unlikely
#define PRINT_RETURN(x) (x)
#define PRINT_RETURN_CONST(x) (x)
#define PRINT_RETURN_FORMAT(x, format, ...) (x)
#define PRINT_NEG_RETURN(x) (x)
#define PRINT_NEG_RETURN_FORMAT(x, format, ...) (x)
#endif
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
#define psync_new(type) (type *)malloc(sizeof(type))
#define psync_new_cnt(type, cnt) (type *)malloc(sizeof(type) * (cnt))
@ -325,55 +237,29 @@ void psync_sql_checkpoint_unlock();
#define psync_sql_lock() psync_sql_do_lock(__FILE__, __LINE__)
#define psync_sql_rdlock() psync_sql_do_rdlock(__FILE__, __LINE__)
#define psync_sql_statement(sql) psync_sql_do_statement(sql, __FILE__, __LINE__)
#define psync_sql_start_transaction() \
psync_sql_do_start_transaction(__FILE__, __LINE__)
#define psync_sql_query_nocache(sql) \
psync_sql_do_query_nocache(sql, __FILE__, __LINE__)
#define psync_sql_start_transaction() psync_sql_do_start_transaction(__FILE__, __LINE__)
#define psync_sql_query_nocache(sql) psync_sql_do_query_nocache(sql, __FILE__, __LINE__)
#define psync_sql_query(sql) psync_sql_do_query(sql, __FILE__, __LINE__)
#define psync_sql_query_rdlock_nocache(sql) \
psync_sql_do_query_rdlock_nocache(sql, __FILE__, __LINE__)
#define psync_sql_query_rdlock(sql) \
psync_sql_do_query_rdlock(sql, __FILE__, __LINE__)
#define psync_sql_query_nolock_nocache(sql) \
psync_sql_do_query_nolock_nocache(sql, __FILE__, __LINE__)
#define psync_sql_query_nolock(sql) \
psync_sql_do_query_nolock(sql, __FILE__, __LINE__)
#define psync_sql_prep_statement_nocache(sql) \
psync_sql_do_prep_statement_nocache(sql, __FILE__, __LINE__)
#define psync_sql_prep_statement(sql) \
psync_sql_do_prep_statement(sql, __FILE__, __LINE__)
#define psync_sql_query_rdlock_nocache(sql) psync_sql_do_query_rdlock_nocache(sql, __FILE__, __LINE__)
#define psync_sql_query_rdlock(sql) psync_sql_do_query_rdlock(sql, __FILE__, __LINE__)
#define psync_sql_query_nolock_nocache(sql) psync_sql_do_query_nolock_nocache(sql, __FILE__, __LINE__)
#define psync_sql_query_nolock(sql) psync_sql_do_query_nolock(sql, __FILE__, __LINE__)
#define psync_sql_prep_statement_nocache(sql) psync_sql_do_prep_statement_nocache(sql, __FILE__, __LINE__)
#define psync_sql_prep_statement(sql) psync_sql_do_prep_statement(sql, __FILE__, __LINE__)
int psync_sql_do_trylock(const char *file, unsigned line);
void psync_sql_do_lock(const char *file, unsigned line);
void psync_sql_do_rdlock(const char *file, unsigned line);
int psync_sql_do_statement(const char *sql, const char *file, unsigned line)
PSYNC_NONNULL(1);
int psync_sql_do_statement(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
int psync_sql_do_start_transaction(const char *file, unsigned line);
psync_sql_res *psync_sql_do_query_nocache(const char *sql, const char *file,
unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query(const char *sql, const char *file,
unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_rdlock_nocache(const char *sql,
const char *file,
unsigned line)
PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_rdlock(const char *sql, const char *file,
unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_nolock_nocache(const char *sql,
const char *file,
unsigned line)
PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_nolock(const char *sql, const char *file,
unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_prep_statement_nocache(const char *sql,
const char *file,
unsigned line)
PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_prep_statement(const char *sql, const char *file,
unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_nocache(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_rdlock_nocache(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_rdlock(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_nolock_nocache(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_query_nolock(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_prep_statement_nocache(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_do_prep_statement(const char *sql, const char *file, unsigned line) PSYNC_NONNULL(1);
void psync_sql_dump_locks();
#else
@ -391,8 +277,7 @@ psync_sql_res *psync_sql_query_rdlock(const char *sql) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_query_nolock(const char *sql) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_query_nolock_nocache(const char *sql) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_prep_statement(const char *sql) PSYNC_NONNULL(1);
psync_sql_res *psync_sql_prep_statement_nocache(const char *sql)
PSYNC_NONNULL(1);
psync_sql_res *psync_sql_prep_statement_nocache(const char *sql) PSYNC_NONNULL(1);
#endif
@ -406,9 +291,7 @@ int psync_sql_sync();
int psync_sql_commit_transaction();
int psync_sql_rollback_transaction();
void psync_sql_transation_add_callbacks(
psync_transaction_callback_t commit_callback,
psync_transaction_callback_t rollback_callback, void *ptr);
void psync_sql_transation_add_callbacks(psync_transaction_callback_t commit_callback, psync_transaction_callback_t rollback_callback, void *ptr);
char *psync_sql_cellstr(const char *sql) PSYNC_NONNULL(1);
int64_t psync_sql_cellint(const char *sql, int64_t dflt) PSYNC_NONNULL(1);
@ -418,26 +301,19 @@ int psync_sql_reset(psync_sql_res *res) PSYNC_NONNULL(1);
int psync_sql_run(psync_sql_res *res) PSYNC_NONNULL(1);
int psync_sql_run_free(psync_sql_res *res) PSYNC_NONNULL(1);
int psync_sql_run_free_nocache(psync_sql_res *res) PSYNC_NONNULL(1);
void psync_sql_bind_uint(psync_sql_res *res, int n, uint64_t val)
PSYNC_NONNULL(1);
void psync_sql_bind_int(psync_sql_res *res, int n, int64_t val)
PSYNC_NONNULL(1);
void psync_sql_bind_double(psync_sql_res *res, int n, double val)
PSYNC_NONNULL(1);
void psync_sql_bind_string(psync_sql_res *res, int n, const char *str)
PSYNC_NONNULL(1);
void psync_sql_bind_lstring(psync_sql_res *res, int n, const char *str,
size_t len) PSYNC_NONNULL(1);
void psync_sql_bind_blob(psync_sql_res *res, int n, const char *str, size_t len)
PSYNC_NONNULL(1);
void psync_sql_bind_uint(psync_sql_res *res, int n, uint64_t val) PSYNC_NONNULL(1);
void psync_sql_bind_int(psync_sql_res *res, int n, int64_t val) PSYNC_NONNULL(1);
void psync_sql_bind_double(psync_sql_res *res, int n, double val) PSYNC_NONNULL(1);
void psync_sql_bind_string(psync_sql_res *res, int n, const char *str) PSYNC_NONNULL(1);
void psync_sql_bind_lstring(psync_sql_res *res, int n, const char *str, size_t len) PSYNC_NONNULL(1);
void psync_sql_bind_blob(psync_sql_res *res, int n, const char *str, size_t len) PSYNC_NONNULL(1);
void psync_sql_bind_null(psync_sql_res *res, int n) PSYNC_NONNULL(1);
void psync_sql_free_result(psync_sql_res *res) PSYNC_NONNULL(1);
void psync_sql_free_result_nocache(psync_sql_res *res) PSYNC_NONNULL(1);
psync_variant_row psync_sql_fetch_row(psync_sql_res *res) PSYNC_NONNULL(1);
psync_str_row psync_sql_fetch_rowstr(psync_sql_res *res) PSYNC_NONNULL(1);
psync_uint_row psync_sql_fetch_rowint(psync_sql_res *res) PSYNC_NONNULL(1);
psync_full_result_int *psync_sql_fetchall_int(psync_sql_res *res)
PSYNC_NONNULL(1);
psync_full_result_int *psync_sql_fetchall_int(psync_sql_res *res) PSYNC_NONNULL(1);
uint32_t psync_sql_affected_rows() PSYNC_PURE;
uint64_t psync_sql_insertid() PSYNC_PURE;
@ -477,10 +353,6 @@ void psync_qpartition(void *base, size_t cnt, size_t sort_first, size_t size,
void psync_try_free_memory();
int psync_debug(const char *file, const char *function, int unsigned line,
int unsigned level, const char *fmt, ...) PSYNC_COLD
PSYNC_FORMAT(printf, 5, 6) PSYNC_NONNULL(5);
uint64_t psync_err_number_expected(const char *file, const char *function,
int unsigned line,
const psync_variant *v) PSYNC_COLD;

View File

@ -40,6 +40,12 @@
#include "pcompiler.h"
// No need to edit this directly, just pass -DPSYNC_SSL_DEBUG_LEVEL as a
// compile option. Possible values are in the range [0, 5]
#ifndef PSYNC_SSL_DEBUG_LEVEL
#define PSYNC_SSL_DEBUG_LEVEL 0
#endif
#define PSYNC_AES256_BLOCK_SIZE 16
#define PSYNC_AES256_KEY_SIZE 32

View File

@ -1,9 +1,13 @@
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/random.h>
#include "plibs.h"
#include "pdbg.h"
#include "putil.h"
// wipe a segment of memory mem of size sz using a DoD 5220.22-M compliant
// 3-pass wipe. the first pass overwrites the memory with zeroes, the second
@ -33,3 +37,48 @@ void putil_wipe(void *mem, size_t sz) {
}
}
}
void time_format(time_t tm, unsigned long ns, char *result) {
static const char month_names[12][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
static const char day_names[7][4] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"};
struct tm dt;
unsigned long y;
ns /= 1000000;
gmtime_r(&tm, &dt);
memcpy(result, day_names[dt.tm_wday], 3);
result += 3;
*result++ = ',';
*result++ = ' ';
*result++ = dt.tm_mday / 10 + '0';
*result++ = dt.tm_mday % 10 + '0';
*result++ = ' ';
memcpy(result, month_names[dt.tm_mon], 3);
result += 3;
*result++ = ' ';
y = dt.tm_year + 1900;
*result++ = '0' + y / 1000;
y = y % 1000;
*result++ = '0' + y / 100;
y = y % 100;
*result++ = '0' + y / 10;
y = y % 10;
*result++ = '0' + y;
*result++ = ' ';
*result++ = dt.tm_hour / 10 + '0';
*result++ = dt.tm_hour % 10 + '0';
*result++ = ':';
*result++ = dt.tm_min / 10 + '0';
*result++ = dt.tm_min % 10 + '0';
*result++ = ':';
*result++ = dt.tm_sec / 10 + '0';
*result++ = dt.tm_sec % 10 + '0';
*result++ = '.';
*result++ = ns / 100 + '0';
*result++ = (ns / 10) % 10 + '0';
*result++ = ns % 10 + '0';
memcpy(result, " +0000", 7); // copies the null byte
}

View File

@ -6,12 +6,15 @@ extern "C" {
#endif
#include <stddef.h>
#include <time.h>
#define NTO_STR(s) TO_STR(s)
#define TO_STR(s) #s
#define VAR_ARRAY(name, type, size) type name[size]
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
void putil_wipe(void *mem, size_t sz);
void time_format(time_t tm, unsigned long ns, char *result);
#ifdef __cplusplus
}

View File

@ -10,6 +10,7 @@
#include "rpcclient.h"
#include "plibs.h"
#include "pdbg.h"
#include "prpc.h"
#include "putil.h"