[go: up one dir, main page]

Menu

[e1ed9f]: / src / common.c  Maximize  Restore  History

Download this file

127 lines (107 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <pthread.h>
#include <execinfo.h>
#include "common.h"
int common_debug_level = 0;
FILE* common_stdlog = NULL;
char common_logfile[256] = "";
pthread_mutex_t m_common = PTHREAD_MUTEX_INITIALIZER;
int common_set_smbnetfs_debug_level(int level){
if ((level < 0) || (level > 10)) return 0;
DPRINTF(8, "level=%d\n", level);
pthread_mutex_lock(&m_common);
common_debug_level = level;
pthread_mutex_unlock(&m_common);
return 1;
}
int common_get_smbnetfs_debug_level(void){
int level;
pthread_mutex_lock(&m_common);
level = common_debug_level;
pthread_mutex_unlock(&m_common);
DPRINTF(8, "level=%d\n", level);
return level;
}
int common_set_log_file(const char *logfile){
int result;
DPRINTF(7, "logfile=%s\n", logfile);
result = 1;
pthread_mutex_lock(&m_common);
if ( ! ((logfile != NULL) && (strcmp(common_logfile, logfile) == 0))){
if (common_stdlog != NULL){
fclose(common_stdlog);
memset(common_logfile, 0, sizeof(common_logfile));
common_stdlog = NULL;
}
if (logfile != NULL)
strncpy(common_logfile, logfile, sizeof(common_logfile) - 1);
if (*common_logfile != '\0'){
common_stdlog = fopen(common_logfile, "a");
if (common_stdlog == NULL){
memset(common_logfile, 0, sizeof(common_logfile));
result = 0;
}
}
}
pthread_mutex_unlock(&m_common);
return result;
}
void common_debug_print(int level, const char *fmt, ...){
va_list ap;
pthread_mutex_lock(&m_common);
if ((level >= 0) && (level <= common_debug_level)){
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);
if (common_stdlog != NULL){
va_start(ap, fmt);
vfprintf(common_stdlog, fmt, ap);
va_end(ap);
fflush(common_stdlog);
}
}
pthread_mutex_unlock(&m_common);
}
void common_print_backtrace(void){
static char buf[256];
int fd;
#ifdef HAVE_BACKTRACE
void *array[200];
size_t size;
#endif /* HAVE_BACKTRACE */
snprintf(buf, sizeof(buf), "%d->%s: dumping ...\n", getpid(), __FUNCTION__);
buf[sizeof(buf) - 2] = '\n';
buf[sizeof(buf) - 1] = '\0';
#ifdef HAVE_BACKTRACE
size = backtrace(array, 200);
#endif /* HAVE_BACKTRACE */
fd = fileno(stderr);
write(fd, buf, strlen(buf));
#ifdef HAVE_BACKTRACE
backtrace_symbols_fd(array, size, fd);
#endif /* HAVE_BACKTRACE */
fsync(fd);
if (common_stdlog != NULL){
fd = fileno(common_stdlog);
write(fd, buf, strlen(buf));
#ifdef HAVE_BACKTRACE
backtrace_symbols_fd(array, size, fd);
#endif /* HAVE_BACKTRACE */
fsync(fd);
}
}
#ifndef HAVE_STRNDUP
char* strndup(const char *s, size_t n){
char *p;
if (strlen(s) <= n) return strdup(s);
if ((p = malloc(n + 1)) == NULL) return NULL;
memcpy(p, s, n);
p[n] = '\0';
return p;
}
#endif /* HAVE_STRNDUP */