#ifndef COMMON_HPP_
#define COMMON_HPP_
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>


#define cstrlen(s) sizeof(s)-1
#define cstrnlen(s) s, cstrlen(s)


extern time_t NOW;
void update_now();

extern const char* log_ctx;
#ifdef LOG_TIME_PREFIX
extern char NOW_STR[cstrlen("Sun Sep 16 01:03:52 1973")+1];
#define LOG(fmt, ...) fprintf(stderr, "%s [%s] " fmt "\n", NOW_STR, log_ctx?:"-", ##__VA_ARGS__)
#else
#define LOG(fmt, ...) fprintf(stderr, "[%s] " fmt "\n", log_ctx?:"-", ##__VA_ARGS__)
#endif
#define LOG_ERRNO(fmt, ...) LOG(fmt " - %d: %s", ##__VA_ARGS__, errno, strerror(errno))


#define VAL(x) #x
#define STRINGIFY(x) VAL(x)


#define INIT_EARLY __attribute__((init_priority(101)))
#define INIT_LATE __attribute__((init_priority(65535)))
#define INLINE inline __attribute__((always_inline))
#define PRINTF_FMT(f,a)  __attribute__((format(printf, f, a)))
#define UNUSED __attribute__((unused))


#define is_letter(c) (((c)>='a' && (c)<='z') || ((c)>='A' && (c)<='Z'))
#define is_number(c) ((c)>='0' && (c)<='9')


#endif