Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • cellframe/libdap
1 result
Show changes
Commits on Source (32)
......@@ -30,10 +30,13 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "portable_endian.h"
typedef uint8_t byte_t;
#if defined(__GNUC__) ||defined (__clang__)
#define DAP_ALIGN_PACKED __attribute__((aligned(1),packed))
#else
......@@ -90,10 +93,6 @@
#define DAP_DUP(a) ( __typeof(a) ret = memcpy(ret,a,sizeof(*a)) )
#endif
#ifndef MAX_PATH
#define MAX_PATH 120
#endif
DAP_STATIC_INLINE void *_dap_aligned_alloc( uintptr_t alignment, uintptr_t size )
{
uintptr_t ptr = (uintptr_t) DAP_MALLOC( size + (alignment * 2) + sizeof(void *) );
......@@ -131,22 +130,6 @@ DAP_STATIC_INLINE void _dap_aligned_free( void *ptr )
#define DAP_PROTOCOL_VERSION 22
#ifndef _WIN32
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef min
#define min MIN
#endif
#ifndef max
#define max MAX
#endif
#endif
#ifndef LOWORD
#define LOWORD( l ) ((uint16_t) (((uintptr_t) (l)) & 0xFFFF))
#define HIWORD( l ) ((uint16_t) ((((uintptr_t) (l)) >> 16) & 0xFFFF))
......@@ -167,10 +150,10 @@ DAP_STATIC_INLINE void _dap_aligned_free( void *ptr )
#define DAP_LOG_HISTORY 1
#define DAP_LOG_HISTORY_STR_SIZE 128
#define DAP_LOG_HISTORY_MAX_STRINGS 4096
#define DAP_LOG_HISTORY_BUFFER_SIZE (DAP_LOG_HISTORY_STR_SIZE * DAP_LOG_HISTORY_MAX_STRINGS)
#define DAP_LOG_HISTORY_M (DAP_LOG_HISTORY_MAX_STRINGS - 1)
//#define DAP_LOG_HISTORY_STR_SIZE 128
//#define DAP_LOG_HISTORY_MAX_STRINGS 4096
//#define DAP_LOG_HISTORY_BUFFER_SIZE (DAP_LOG_HISTORY_STR_SIZE * DAP_LOG_HISTORY_MAX_STRINGS)
//#define DAP_LOG_HISTORY_M (DAP_LOG_HISTORY_MAX_STRINGS - 1)
#ifdef _WIN32
#define dap_sscanf __mingw_sscanf
......@@ -208,7 +191,7 @@ DAP_STATIC_INLINE void _dap_aligned_free( void *ptr )
#define dap_vasprintf vasprintf
#endif
typedef int DAP_SpinLock;
typedef int dap_spinlock_t;
/**
* @brief The log_level enum
......@@ -241,6 +224,20 @@ typedef struct dap_log_history_str_s {
extern "C" {
#endif
#ifndef MAX_PATH
#define MAX_PATH 120
#endif
#ifndef _WIN32
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#endif
extern uint16_t htoa_lut256[ 256 ];
#define dap_htoa64( out, in, len ) \
......@@ -303,25 +300,28 @@ static const uint16_t s_ascii_table_data[256] = {
#define dap_ascii_isspace(c) (s_ascii_table_data[(unsigned char) (c)] & DAP_ASCII_SPACE) != 0
#define dap_ascii_isalpha(c) (s_ascii_table_data[(unsigned char) (c)] & DAP_ASCII_ALPHA) != 0
void DAP_Sleep( uint32_t ms );
void dap_sleep( uint32_t ms );
DAP_STATIC_INLINE bool DAP_AtomicTryLock( DAP_SpinLock *lock )
DAP_STATIC_INLINE bool DAP_AtomicTryLock( dap_spinlock_t *lock )
{
return (__sync_lock_test_and_set(lock, 1) == 0);
}
DAP_STATIC_INLINE void DAP_AtomicLock( DAP_SpinLock *lock )
DAP_STATIC_INLINE void DAP_AtomicLock( dap_spinlock_t *lock )
{
while ( !DAP_AtomicTryLock(lock) ) {
DAP_Sleep( 0 );
dap_sleep( 0 );
}
}
DAP_STATIC_INLINE void DAP_AtomicUnlock( DAP_SpinLock *lock )
DAP_STATIC_INLINE void DAP_AtomicUnlock( dap_spinlock_t *lock )
{
__sync_lock_release( lock );
}
extern char s_sys_dir_path[MAX_PATH];
extern unsigned int l_sys_dir_path_len;
//int dap_common_init( const char * a_log_file );
int dap_common_init( const char *console_title, const char *a_log_file );
......@@ -332,11 +332,16 @@ void dap_log_set_max_item(unsigned int a_max);
// get logs from list
char *dap_log_get_item(time_t a_start_time, int a_limit);
void _log_it( const char * log_tag, uint32_t taglen, enum dap_log_level, const char * format,... );
void _vlog_it( const char * log_tag, uint32_t taglen, enum dap_log_level, const char * format, va_list ap );
#ifdef DAP_LOG_MT
void _log_it( const char * log_tag, enum dap_log_level, const char * format,... );
#define log_it( _log_level, ...) _log_it( LOG_TAG, _log_level, ##__VA_ARGS__)
#else
void _log_it2( const char *log_tag, enum dap_log_level ll, const char *fmt,... );
#define log_it( _log_level, ...) _log_it2( LOG_TAG, _log_level, ##__VA_ARGS__)
#endif
//void _vlog_it( const char * log_tag, uint32_t taglen, enum dap_log_level, const char * format, va_list ap );
#define log_it( _log_level, ...) _log_it( LOG_TAG, sizeof(LOG_TAG)-1,_log_level, ##__VA_ARGS__)
#define vlog_it( a_log_level, a_format, a_ap ) _vlog_it( LOG_TAG, sizeof(LOG_TAG)-1, a_log_level, a_format, a_ap )
//#define vlog_it( a_log_level, a_format, a_ap ) _vlog_it( LOG_TAG, sizeof(LOG_TAG)-1, a_log_level, a_format, a_ap )
const char * log_error(void);
void dap_log_level_set(enum dap_log_level ll);
......
......@@ -29,7 +29,6 @@
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
......
......@@ -25,3 +25,4 @@ typedef union dap_uint128{
#endif
} dap_uint128_t;
......@@ -10,6 +10,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define dap_return_if_fail(expr) {if(!(expr)) {return;}}
#define dap_return_val_if_fail(expr,val) {if(!(expr)) {return (val);}}
......@@ -33,9 +34,7 @@
#define clamp(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#ifdef _WIN32
char *strptime( char *buff, const char *fmt, struct tm *tm );
#endif
size_t dap_strlen(const char *a_str);
......@@ -74,6 +73,10 @@ char* dap_strup(const char *a_str, ssize_t a_len);
char* dap_strdown(const char *a_str, ssize_t a_len);
char* dap_strreverse(char *a_string);
#ifdef _WIN32
char *strndup(char *str, unsigned long len);
#endif
#define DAP_USEC_PER_SEC 1000000
void dap_usleep(time_t a_microseconds);
......@@ -12,6 +12,7 @@ win32 {
LIBS += -lpsapi
}
DEFINES += DAP_LOG_MT
HEADERS += $$PWD/include/dap_common.h \
$$PWD/include/dap_config.h \
$$PWD/include/dap_math_ops.h \
......
......@@ -2,7 +2,7 @@
* Authors:
* Dmitriy A. Gearasimov <kahovski@gmail.com>
* DeM Labs Inc. https://demlabs.net
* DeM Labs Open source community https://github.com/demlabsinc
* DeM Labs Open source community https://gitlab.demlabs.net/cellframe
* Copyright (c) 2017-2019
* All rights reserved.
......@@ -21,7 +21,7 @@
You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <time.h> /* 'nanosleep' */
......@@ -30,7 +30,8 @@
#include <stdarg.h>
#include <assert.h>
#include <stdint.h>
#include <stdatomic.h>
#include "utlist.h"
#include <errno.h>
#ifdef DAP_OS_ANDROID
......@@ -61,37 +62,36 @@
#include "dap_strfuncs.h"
#include "dap_string.h"
#include "dap_list.h"
#include "dap_file_utils.h"
#include "dap_lut.h"
#define DAP_LOG_USE_SPINLOCK 1
#define DAP_LOG_USE_SPINLOCK 0
#define DAP_LOG_HISTORY 1
#define LAST_ERROR_MAX 255
#define LOG_TAG "dap_common"
const char *log_level_tag[ 16 ] = {
" [DBG] [ ", // L_DEBUG = 0
" [INF] [ ", // L_INFO = 1,
" [ * ] [ ", // L_NOTICE = 2,
" [MSG] [ ", // L_MESSAGE = 3,
" [DAP] [ ", // L_DAP = 4,
" [WRN] [ ", // L_WARNING = 5,
" [ATT] [ ", // L_ATT = 6,
" [ERR] [ ", // L_ERROR = 7,
" [ ! ] [ ", // L_CRITICAL = 8,
" [---] [ ", // = 9
" [---] [ ", // = 10
" [---] [ ", // = 11
" [---] [ ", // = 12
" [---] [ ", // = 13
" [---] [ ", // = 14
" [---] [ ", // = 15
static const char *s_log_level_tag[ 16 ] = {
" [DBG] ", // L_DEBUG = 0
" [INF] ", // L_INFO = 1,
" [ * ] ", // L_NOTICE = 2,
" [MSG] ", // L_MESSAGE = 3,
" [DAP] ", // L_DAP = 4,
" [WRN] ", // L_WARNING = 5,
" [ATT] ", // L_ATT = 6,
" [ERR] ", // L_ERROR = 7,
" [ ! ] ", // L_CRITICAL = 8,
" [---] ", // = 9
" [---] ", // = 10
" [---] ", // = 11
" [---] ", // = 12
" [---] ", // = 13
" [---] ", // = 14
" [---] ", // = 15
};
const char *ansi_seq_color[ 16 ] = {
const char *s_ansi_seq_color[ 16 ] = {
"\x1b[0;37;40m", // L_DEBUG = 0
"\x1b[1;32;40m", // L_INFO = 2,
......@@ -111,12 +111,9 @@ const char *ansi_seq_color[ 16 ] = {
"", // = 15
};
#ifdef _WIN32
OSVERSIONINFO win32_osvi;
bool bUseANSIEscapeSequences = false;
HANDLE hWin32ConOut = INVALID_HANDLE_VALUE;
static unsigned int s_ansi_seq_color_len[16] = {0};
#ifdef _WIN32
WORD log_level_colors[ 16 ] = {
7, // L_DEBUG
10, // L_INFO
......@@ -137,73 +134,48 @@ const char *ansi_seq_color[ 16 ] = {
};
#endif
uint32_t ansi_seq_color_len[ 16 ];
static volatile bool s_log_term_signal = false;
unsigned int l_sys_dir_path_len = 0;
char s_sys_dir_path[MAX_PATH] = {'\0'};
static char s_last_error[LAST_ERROR_MAX] = {'\0'},
s_log_file_path[MAX_PATH] = {'\0'},
s_log_tag_fmt_str[10] = {'\0'};
static char s_last_error[LAST_ERROR_MAX] = {0};
static enum dap_log_level dap_log_level = L_DEBUG;
static FILE *s_log_file = NULL;
static char *s_log_file_path = NULL;
static char log_tag_fmt_str[10];
#ifdef DAP_LOG_HISTORY
static pthread_mutex_t s_list_logs_mutex = PTHREAD_MUTEX_INITIALIZER;
static uint32_t logh_total = 0; // log history size
static uint32_t logh_outindex = 0;
static uint8_t *logh_buffer = NULL;
static uint8_t *temp_buffer = NULL;
static uint8_t *end_of_logh_buffer = NULL;
static dap_log_history_str_t *log_history = NULL;
#endif
#if DAP_LOG_USE_SPINLOCK
DAP_SpinLock log_spinlock;
static dap_spinlock_t log_spinlock;
#else
static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t s_log_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
static pthread_cond_t s_log_cond = PTHREAD_COND_INITIALIZER;
static volatile int count = 0;
typedef struct dap_log_str_s {
time_t t;
uint8_t *str;
uint32_t len;
uint8_t tag;
} dap_log_str_t;
#define DAP_LOG_STR_SIZE 128
#define DAP_LOG_MAX_STRINGS 32768
#define DAP_LOG_BUFFER_SIZE (DAP_LOG_STR_SIZE * DAP_LOG_MAX_STRINGS)
static uint32_t log_outindex = 0;
static uint8_t *log_buffer = NULL;
static dap_log_str_t *log_strs = NULL;
static pthread_t s_log_thread = 0;
static void *s_log_thread_proc(void *arg);
static pthread_t log_thread;
static bool log_term_signal = false;
static uint32_t log_page = 0;
typedef struct log_str_t {
char str[1000];
unsigned int offset;
struct log_str_t *prev, *next;
} log_str_t;
static time_t g_start_time = 0;
static volatile time_t g_time;
static bool bOverflow = false;
static DAP_ALIGNED(32) uint8_t cdatatime[ 64 ];
static log_str_t *log_buffer = NULL;
static void *log_thread_proc( void *arg );
DAP_STATIC_INLINE DAP_UpdateLogTime( uint32_t lp )
{
time_t t = time( NULL );
struct tm *tmptime = localtime( &t );
strftime( (char *)&cdatatime[lp*32], 32, "[%x-%X]", tmptime );
// printf("Time updated %s page %u\n", (char *)&cdatatime[lp*32], log_page );
DAP_STATIC_INLINE void s_update_log_time(char *a_datetime_str) {
time_t t = time(NULL);
struct tm *tmptime = localtime(&t);
strftime(a_datetime_str, 32, "[%x-%X]", tmptime);
}
/**
* @brief set_log_level Sets the logging level
* @param[in] ll logging level
*/
void dap_log_level_set( enum dap_log_level ll ) {
dap_log_level = ll;
void dap_log_level_set( enum dap_log_level a_ll ) {
dap_log_level = a_ll;
}
enum dap_log_level dap_log_level_get( void ) {
......@@ -214,18 +186,13 @@ enum dap_log_level dap_log_level_get( void ) {
* @brief dap_set_log_tag_width Sets the length of the label
* @param[in] width Length not more than 99
*/
void dap_set_log_tag_width(size_t width) {
void dap_set_log_tag_width(size_t a_width) {
if (width > 99) {
dap_fprintf(stderr,"Can't set width %zd", width);
if (a_width > 99) {
dap_fprintf(stderr,"Can't set width %zd", a_width);
return;
}
// construct new log_tag_fmt_str
strcpy( log_tag_fmt_str, "[%" );
strcat( log_tag_fmt_str, dap_itoa((int)width) );
// strcat( log_tag_fmt_str, itoa((int)width) );
strcat( log_tag_fmt_str, "s]\t" );
dap_snprintf(s_log_tag_fmt_str,sizeof (s_log_tag_fmt_str), "[%%%zds]\t",a_width);
}
/**
......@@ -233,568 +200,98 @@ void dap_set_log_tag_width(size_t width) {
* @param[in] a_log_file
* @return
*/
int dap_common_init( const char *console_title, const char *a_log_file )
{
srand( (unsigned int)time(NULL) );
#ifdef _WIN32
SetupConsole( console_title, L"Lucida Console", 12, 20 );
#endif
g_start_time = time( NULL );
// init default log tag 8 width
strcpy( log_tag_fmt_str, "[%8s]\t");
#if DAP_LOG_HISTORY
logh_buffer = (uint8_t *)DAP_MALLOC( DAP_LOG_HISTORY_BUFFER_SIZE );
if ( !logh_buffer )
goto err;
end_of_logh_buffer = logh_buffer + DAP_LOG_HISTORY_BUFFER_SIZE;
log_history = (dap_log_history_str_t *)DAP_MALLOC( DAP_LOG_HISTORY_MAX_STRINGS * sizeof(dap_log_history_str_t) );
if ( !log_history )
goto err;
int dap_common_init( const char *a_console_title, const char *a_log_filename ) {
for ( uint32_t i = 0; i < DAP_LOG_HISTORY_MAX_STRINGS; ++ i ) {
log_history[ i ].t = 0;
log_history[ i ].str = logh_buffer + DAP_LOG_HISTORY_STR_SIZE * i;
// init randomer
srand( (unsigned int)time(NULL) );
(void) a_console_title;
strncpy( s_log_tag_fmt_str, "[%s]\t",sizeof (s_log_tag_fmt_str));
for (int i = 0; i < 16; ++i)
s_ansi_seq_color_len[i] =(unsigned int) strlen(s_ansi_seq_color[i]);
if ( a_log_filename ) {
s_log_file = fopen( a_log_filename , "a" );
if ( s_log_file == NULL ) {
dap_fprintf( stderr, "Can't open log file %s to append\n", a_log_filename );
return -1;
}
#endif
log_buffer = (uint8_t *)DAP_MALLOC( DAP_LOG_BUFFER_SIZE * 2 );
if ( !log_buffer )
goto err;
log_strs = (dap_log_str_t *)DAP_MALLOC( DAP_LOG_MAX_STRINGS * sizeof(dap_log_str_t) * 2 );
if ( !log_strs )
goto err;
for ( uint32_t i = 0; i < DAP_LOG_MAX_STRINGS * 2; ++ i ) {
log_strs[ i ].str = log_buffer + DAP_LOG_STR_SIZE * i;
}
temp_buffer = (uint8_t *)DAP_MALLOC( 65536 );
if ( !logh_buffer )
goto err;
for ( uint32_t i = 0; i < 16; ++ i )
ansi_seq_color_len[ i ] = strlen( ansi_seq_color[i] );
#ifdef _WIN32
memset( &win32_osvi, 0, sizeof(OSVERSIONINFO) );
win32_osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
GetVersionEx( (OSVERSIONINFO *)&win32_osvi );
bUseANSIEscapeSequences = (win32_osvi.dwMajorVersion >= 10);
//if ( !bUseANSIEscapeSequences )
hWin32ConOut = GetStdHandle( STD_OUTPUT_HANDLE );
#if 0
printf( "Windows version %u.%u platformID %u \n",
win32_osvi.dwMajorVersion,
win32_osvi.dwMinorVersion,
win32_osvi.dwPlatformId );
#endif
#endif
if ( !a_log_file )
return 0;
s_log_file = fopen( a_log_file , "a" );
if ( s_log_file == NULL ) {
dap_fprintf( stderr, "Can't open log file %s to append\n", a_log_file );
return -1;
dap_stpcpy(s_log_file_path, a_log_filename);
}
if(s_log_file_path)
DAP_DELETE(s_log_file_path);
s_log_file_path = dap_strdup(a_log_file);
log_page = 0;
log_outindex = 0;
DAP_UpdateLogTime( 0 );
log_term_signal = false;
pthread_create( &log_thread, NULL, log_thread_proc, NULL );
pthread_create( &s_log_thread, NULL, s_log_thread_proc, NULL );
return 0;
err:
printf( "Fatal Error: Out of memory!\n" );
dap_common_deinit( );
return -1;
}
/**
* @brief dap_common_deinit Deinitialise
*/
void dap_common_deinit( )
{
printf("dap_common_deinit( )\n");
log_term_signal = true;
pthread_join( log_thread, NULL );
if ( s_log_file )
fclose( s_log_file );
if(s_log_file_path){
DAP_DELETE(s_log_file_path);
s_log_file_path = NULL;
}
if ( temp_buffer )
DAP_FREE( temp_buffer );
if ( log_strs )
DAP_FREE( log_strs );
if ( log_buffer )
DAP_FREE( log_buffer );
#if DAP_LOG_HISTORY
if ( log_history )
DAP_FREE( log_history );
if ( logh_buffer )
DAP_FREE( logh_buffer );
#endif
}
#if DAP_LOG_HISTORY
void log_log( char *str, uint32_t len, time_t t )
{
pthread_mutex_lock( &s_list_logs_mutex );
while( len ) {
uint8_t *out = log_history[ logh_outindex ].str;
uint32_t ilen = len;
if ( out + len >= end_of_logh_buffer )
ilen = end_of_logh_buffer - out;
memcpy( out, str, ilen );
len -= ilen;
do {
log_history[ logh_outindex ].t = t;
if ( ilen >= DAP_LOG_HISTORY_STR_SIZE ) {
log_history[ logh_outindex ].len = DAP_LOG_HISTORY_STR_SIZE;
ilen -= DAP_LOG_HISTORY_STR_SIZE;
}
else {
log_history[ logh_outindex ].len = ilen;
ilen = 0;
}
++ logh_outindex;
logh_outindex &= DAP_LOG_HISTORY_M;
if ( logh_total < DAP_LOG_HISTORY_MAX_STRINGS )
++ logh_total;
} while( ilen );
}
pthread_mutex_unlock( &s_list_logs_mutex );
return;
void dap_common_deinit( ) {
s_log_term_signal = true;
pthread_join(s_log_thread, NULL);
if (s_log_file)
fclose(s_log_file);
}
uint32_t logh_since( time_t t )
{
uint32_t bi = 0;
uint32_t si = logh_total >> 1;
uint32_t li = (logh_outindex - 1) & DAP_LOG_HISTORY_M;
if ( log_history[li].t < t ) // no new logs
return 0xFFFFFFFF;
if (logh_total >= DAP_LOG_HISTORY_MAX_STRINGS )
bi = logh_outindex;
if ( log_history[bi].t >= t ) // all logs is new
return bi;
while( si ) {
if ( log_history[(bi + si) & DAP_LOG_HISTORY_M].t < t )
bi += si;
si >>= 1;
}
return (bi + si + 1) & DAP_LOG_HISTORY_M;
}
/**
uint32_t logh_since( time_t t )
{
uint32_t li = (logh_outindex - 1) & DAP_LOG_HISTORY_M;
uint32_t count = logh_total;
uint32_t fi = 0;
uint32_t si = 0;
if ( log_history[li].t < t ) // no new logs
return 0xFFFFFFFF;
if (logh_total >= DAP_LOG_HISTORY_MAX_STRINGS )
fi = logh_outindex;
if ( log_history[fi].t >= t ) // all logs is new
return fi;
do {
if ( log_history[li].t < t ) {
si = li;
break;
}
li = (li - 1) & DAP_LOG_HISTORY_M;
} while ( --count );
return (si + 1) & DAP_LOG_HISTORY_M;
}
**/
#endif
/*
* Get logs from list
* @brief s_log_thread_proc
* @param arg
* @return
*/
char *dap_log_get_item( time_t a_time, int a_limit )
{
#if !DAP_LOG_HISTORY
char *res = (char *)DAP_MALLOC( 64 );
if ( !res )
return res;
strcpy( res, "DAP_LOG_HISTORY is disabled" );
return res;
#else
uint32_t l_count;
uint32_t si;
char *res, *out;
time_t a_start_time;
a_start_time = time( NULL );
if ( a_time > a_start_time )
a_start_time = 0;
else
a_start_time -= a_time;
pthread_mutex_lock( &s_list_logs_mutex );
l_count = logh_total;
if ( l_count > (uint32_t)a_limit )
l_count = a_limit;
if ( !l_count ) {
pthread_mutex_unlock( &s_list_logs_mutex );
return NULL;
}
si = logh_since( a_start_time );
if ( si == 0xFFFFFFFF || log_history[ si ].t < a_start_time ) {// no new logs
pthread_mutex_unlock( &s_list_logs_mutex );
return NULL;
}
out = res = (char *)DAP_MALLOC( l_count * DAP_LOG_HISTORY_STR_SIZE + 1 );
if ( !res ) {
pthread_mutex_unlock( &s_list_logs_mutex );
return NULL;
}
do {
memcpy( out, log_history[ si ].str, log_history[ si ].len );
out += log_history[ si ].len;
si = (si + 1) & DAP_LOG_HISTORY_M;
if ( si == logh_outindex || log_history[ si ].t < a_start_time )
break;
} while ( --l_count );
*out = 0;
pthread_mutex_unlock( &s_list_logs_mutex );
return res;
#endif
}
static void *log_thread_proc( void *arg )
{
int32_t outlogstrs = 0, tmp, n;
dap_log_str_t *logstr;
while ( !log_term_signal ) {
DAP_UpdateLogTime( log_page^1 );
#if DAP_LOG_USE_SPINLOCK
DAP_AtomicLock( &log_spinlock );
#else
pthread_mutex_lock( &log_mutex );
#endif
if ( !log_outindex ) {
log_page ^= 1;
#if DAP_LOG_USE_SPINLOCK
DAP_AtomicUnlock( &log_spinlock );
#else
pthread_mutex_unlock( &log_mutex );
#endif
// printf("log_thread_proc: nothing to log. Sleeping...\n" );
DAP_Sleep( 10 );
continue;
static void *s_log_thread_proc(void *arg) {
for ( ; !s_log_term_signal; ) {
pthread_mutex_lock(&s_log_mutex);
for ( ; count == 0; ) {
pthread_cond_wait(&s_log_cond, &s_log_mutex);
}
n = outlogstrs = log_outindex;
log_outindex = 0;
logstr = &log_strs[ log_page * DAP_LOG_MAX_STRINGS ];
log_page ^= 1;
#if DAP_LOG_USE_SPINLOCK
DAP_AtomicUnlock( &log_spinlock );
#else
pthread_mutex_unlock( &log_mutex );
#endif
// printf("log_thread_proc: outlogstrs: %u\n", outlogstrs );
do {
#ifdef DAP_OS_ANDROID
buf2[ len ] = 0;
__android_log_write( ANDROID_LOG_INFO, DAP_BRAND, buf0 + msg_offset );
#endif
#if 1
#ifdef _WIN32
// if ( !bUseANSIEscapeSequences )
SetConsoleTextAttribute( hWin32ConOut, log_level_colors[logstr->tag] );
// WriteConsole( hWin32ConOut, buf0 + time_offset, len - time_offset, &tmp, NULL );
// fwrite( buf0 + time_offset, len - time_offset, 1, stdout );
WriteFile( hWin32ConOut, logstr->str, logstr->len, (LPDWORD)&tmp, NULL );
#else
fwrite( ansi_seq_color[logstr->tag], 10, 1, stdout );
fwrite( logstr->str, logstr->len, 1, stdout );
#endif
#endif
if(s_log_file) {
if(!dap_file_test(s_log_file_path)) {
fclose(s_log_file);
s_log_file = fopen(s_log_file_path, "a");
}
if(s_log_file) {
fwrite(logstr->str, logstr->len, 1, s_log_file);
fflush(s_log_file);
}
log_str_t *elem, *tmp;
if(s_log_file) {
if(!dap_file_test(s_log_file_path)) {
fclose(s_log_file);
s_log_file = fopen(s_log_file_path, "a");
}
// fwrite( "1234567890", 5, 1, stdout );
uint32_t al = ROUNDUP(logstr->len, 128 ) >> 7;
logstr += al;
n -= al;
} while ( n > 0 );
if ( outlogstrs < 1024 ) {
DAP_Sleep( 10 );
}
}
// printf("log_thread_proc Finished! Overflows? = %u\n", bOverflow );
return NULL;
}
DAP_STATIC_INLINE void _log_enqueue( const char *log_tag, uint32_t taglen, uint8_t *msg, uint32_t len , uint32_t ll )
{
uint32_t total_len = len + 19 + 8 + taglen + 3;
uint32_t al = ROUNDUP( total_len, 128 ) >> 7;
if ( !al )
return;
//[07/27/19-03:22:45] [INF] [db_cdb]
DL_FOREACH_SAFE(log_buffer, elem, tmp) {
if(s_log_file)
fwrite(elem->str + elem->offset, strlen(elem->str) - elem->offset, 1, s_log_file);
fwrite(elem->str, strlen(elem->str), 1, stdout);
DL_DELETE(log_buffer, elem);
DAP_FREE(elem);
--count;
if(s_log_file)
fflush(s_log_file);
fflush(stdout);
}
#if DAP_LOG_USE_SPINLOCK
DAP_AtomicLock( &log_spinlock );
#else
pthread_mutex_lock( &log_mutex );
#endif
if ( log_outindex + (al-1) >= DAP_LOG_MAX_STRINGS ) {
if ( !bOverflow ) {
bOverflow = true;
printf("Overflow!!!\n");
}
#if DAP_LOG_USE_SPINLOCK
DAP_AtomicUnlock( &log_spinlock );
#else
pthread_mutex_unlock( &log_mutex );
#endif
return;
pthread_mutex_unlock(&s_log_mutex);
}
dap_log_str_t *logstr = &log_strs[ log_page * DAP_LOG_MAX_STRINGS + log_outindex ];
uint8_t *out = logstr->str;
memcpy( out, &cdatatime[log_page*32], 19 );
out += 19;
memcpy( out, log_level_tag[ll], 8 );
out += 8;
memcpy( out, log_tag, taglen );
out += taglen;
*out ++ = ']';
*out ++ = ' ';
memcpy( out, msg, len );
out += len;
*out = 10;
logstr->t = g_time;
logstr->len = total_len;
logstr->tag = ll;
log_outindex += al;
#if DAP_LOG_USE_SPINLOCK
DAP_AtomicUnlock( &log_spinlock );
#else
pthread_mutex_unlock( &log_mutex );
#endif
pthread_exit(NULL);
}
void _log_it( const char *log_tag, uint32_t taglen, enum dap_log_level ll, const char *fmt,... )
{
DAP_ALIGNED(32) uint8_t buf0[ 16384 ];
uint32_t len;
void _log_it(const char *log_tag, enum dap_log_level ll, const char *fmt, ...) {
if ( ll < dap_log_level || ll >= 16 || !log_tag )
return;
log_str_t *log_string = DAP_NEW_Z(log_str_t);
strncpy(log_string->str, s_ansi_seq_color[ll],sizeof (log_string->str)-1);
log_string->offset = s_ansi_seq_color_len[ll];
s_update_log_time(log_string->str + log_string->offset);
size_t offset = strlen(log_string->str);
offset += dap_snprintf(log_string->str + offset, sizeof (log_string->str) -offset, "%s[%s%s", s_log_level_tag[ll], log_tag, "] ");
va_list va;
va_start( va, fmt );
len = dap_vsprintf( (char *)&buf0[0], fmt, va );
offset += dap_vsnprintf(log_string->str + offset,sizeof (log_string->str) -offset, fmt, va);
va_end( va );
_log_enqueue( log_tag, taglen, &buf0[0], len, ll );
return;
}
/**
* @brief _log_it Writes information to the log
* @param[in] log_tag Tag
* @param[in] ll Log level
* @param[in] format
*/
void _log_it2( const char *log_tag, enum dap_log_level ll, const char *fmt,... )
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
uint8_t *buf0 = temp_buffer;
uint32_t len, tmp,
time_offset,
tag_offset,
msg_offset;
if ( ll < dap_log_level || ll >= 16 || !log_tag )
return;
// time_t t = time( NULL ) - g_start_time;
time_t t = time( NULL );
pthread_mutex_lock( &mutex );
memcpy( buf0, ansi_seq_color[ll], ansi_seq_color_len[ll] );
time_offset = ansi_seq_color_len[ll];
struct tm *tmptime = localtime( &t );
len = strftime( (char *)(buf0 + time_offset), 65536, "[%x-%X]", tmptime );
tag_offset = time_offset + len;
memcpy( buf0 + tag_offset, log_level_tag[ll], 8 );
memcpy( buf0 + tag_offset + 8, log_level_tag[ll], 8 );
msg_offset = tag_offset + 8;
while ( *log_tag )
buf0[ msg_offset ++ ] = *log_tag ++;
buf0[ msg_offset ++ ] = ']';
buf0[ msg_offset ++ ] = ' ';
// buf0[ msg_offset ++ ] = 9;
va_list va;
va_start( va, fmt );
len = dap_vsprintf( (char * __restrict )(buf0 + msg_offset), fmt, va );
va_end( va );
len += msg_offset;
#ifdef DAP_OS_ANDROID
buf2[ len ] = 0;
__android_log_write( ANDROID_LOG_INFO, DAP_BRAND, buf0 + msg_offset );
#endif
buf0[ len++ ] = 10;
if ( s_log_file )
fwrite( buf0 + time_offset, len - time_offset, 1, s_log_file );
// buf0[ len++ ] = 0;
log_log( (char *)(buf0 + time_offset), len - time_offset, t );
#if 0
#ifdef _WIN32
// if ( !bUseANSIEscapeSequences )
SetConsoleTextAttribute( hWin32ConOut, log_level_colors[ll] );
// WriteConsole( hWin32ConOut, buf0 + time_offset, len - time_offset, &tmp, NULL );
// fwrite( buf0 + time_offset, len - time_offset, 1, stdout );
WriteFile( hWin32ConOut, buf0 + time_offset, len - time_offset, (LPDWORD)&tmp, NULL );
#else
fwrite( buf0, len, 1, stdout );
#endif
#endif
// printf("\x1b[0m\n");
pthread_mutex_unlock( &mutex );
memcpy(&log_string->str[offset], "\n", 1);
pthread_mutex_lock(&s_log_mutex);
DL_APPEND(log_buffer, log_string);
++count;
pthread_cond_signal(&s_log_cond);
pthread_mutex_unlock(&s_log_mutex);
}
void DAP_Sleep( uint32_t ms )
{
#ifdef _WIN32
Sleep( ms );
#else
int was_error;
struct timespec elapsed, tv;
elapsed.tv_sec = ms / 1000;
elapsed.tv_nsec = (ms % 1000) * 1000000;
do {
errno = 0;
tv.tv_sec = elapsed.tv_sec;
tv.tv_nsec = elapsed.tv_nsec;
was_error = nanosleep( &tv, &elapsed );
} while( was_error && (errno == EINTR) );
#endif
char *dap_log_get_item(time_t a_start_time, int a_limit) {
return NULL; // TODO
}
/**
......@@ -1106,8 +603,6 @@ size_t dap_bin2hex(char *a_out, const void *a_in, size_t a_len)
return a_len;
}
// !!!!!!!!!!!!!!!!!!!
/**
* Convert binhex encoded data to binary data
*
......@@ -1133,8 +628,6 @@ size_t dap_hex2bin(uint8_t *a_out, const char *a_in, size_t a_len)
return a_len;
}
// !!!!!!!!!!!!!!!!!!!
/**
* Convert string to digit
*/
......
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "dap_file_utils.h"
......@@ -60,7 +61,7 @@ int dap_config_init(const char * a_configs_path)
}
#endif
if(dap_dir_test(a_configs_path) || !dap_mkdir_with_parents(a_configs_path)) {
strncpy(s_configs_path, a_configs_path,sizeof(s_configs_path));
strncpy(s_configs_path, a_configs_path,sizeof(s_configs_path)-1);
return 0;
}
}
......@@ -112,12 +113,14 @@ dap_config_t * dap_config_open(const char * a_name)
snprintf(l_config_path,l_config_path_size_max, "%s/%s.cfg",s_configs_path,a_name);
FILE * f = fopen(l_config_path,"r");
if ( f ){
fseek(f, 0, SEEK_END);
long buf_len = ftell(f);
char buf[buf_len];
fseek(f, 0L, SEEK_SET);
log_it(L_DEBUG,"Opened config %s",a_name);
ret = DAP_NEW_Z(dap_config_t);
dap_config_internal_t * l_config_internal = DAP_NEW_Z(dap_config_internal_t);
ret->_internal = l_config_internal;
char buf[100024];
size_t l_global_offset=0;
size_t l_buf_size=0;
size_t l_buf_pos_line_start=0;
......@@ -126,7 +129,7 @@ dap_config_t * dap_config_open(const char * a_name)
bool l_is_space_now = false;
while ( feof(f)==0){ // Break on lines
size_t i;
l_global_offset += (l_buf_size = fread(buf,1,sizeof(buf),f) );
l_global_offset += (l_buf_size = fread(buf, 1, buf_len, f) );
for (i=0; i< l_buf_size; i++){
if( (buf[i] == '\r') || (buf[i] == '\n' ) ){
if( ! l_is_space_now){
......
......@@ -6,38 +6,12 @@
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "dap_common.h"
#include "dap_strfuncs.h"
#ifdef _WIN32
static inline char *strndup(char *str, int len) {
char *buf = (char*)malloc(len + 1);
memcpy(buf, str, len);
buf[len] = 0;
return buf;
}
#endif
#ifdef _WIN32
char *strptime( char *buff, const char *fmt, struct tm *tm )
{
uint32_t len = strlen( buff );
dap_sscanf( buff,"%u.%u.%u_%u.%u.%u",&tm->tm_year, &tm->tm_mon, &tm->tm_mday, &tm->tm_hour, &tm->tm_min, &tm->tm_sec );
tm->tm_year += 2000;
return buff + len;
}
#endif
/**
* dap_strlen:
* @a_str: (nullable): the string
......@@ -708,6 +682,25 @@ char* dap_strreverse(char *a_string)
return a_string;
}
#ifdef _WIN32
char *strptime( char *buff, const char *fmt, struct tm *tm ) {
uint32_t len = strlen( buff );
dap_sscanf( buff,"%u.%u.%u_%u.%u.%u",&tm->tm_year, &tm->tm_mon, &tm->tm_mday, &tm->tm_hour, &tm->tm_min, &tm->tm_sec );
tm->tm_year += 2000;
return buff + len;
}
char *strndup(char *str, unsigned long len) {
char *buf = (char*)memchr(str, '\0', len);
if (buf)
len = buf - str;
buf = (char*)malloc(len + 1);
memcpy(buf, str, len);
buf[len] = '\0';
return buf;
}
#endif
/**
* dap_usleep:
* @a_microseconds: number of microseconds to pause
......
......@@ -5,6 +5,10 @@
#include <windows.h>
#include <tchar.h>
#ifdef __cplusplus
extern "C" {
#endif
wchar_t* readRegKey(HKEY hKey, LPCWSTR regSubKey, LPCWSTR val);
wchar_t* getTapGUID();
wchar_t* getTapName();
......@@ -12,4 +16,8 @@ wchar_t* getUserSID(LPCWSTR homePath);
char* regGetUsrPath();
#ifdef __cplusplus
}
#endif
#endif