"Fossies" - the Fresh Open Source Software Archive

Member "smbnetfs-0.6.3/src/common.c" (2 Jan 2018, 2445 Bytes) of package /linux/misc/smbnetfs-0.6.3.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 0.6.0_vs_0.6.1.

    1 #include "config.h"
    2 #include <stdio.h>
    3 #include <stdlib.h>
    4 #include <stdarg.h>
    5 #include <string.h>
    6 #include <pthread.h>
    7 #include <execinfo.h>
    8 #include <sys/types.h>
    9 #include <sys/stat.h>
   10 #include <fcntl.h>
   11 #include <errno.h>
   12 
   13 #include "common.h"
   14 
   15 int         common_debug_level  = 0;
   16 static int      common_logfd[2]     = {2, -1};
   17 static char     common_logfile[256] = "";
   18 static pthread_mutex_t  m_common        = PTHREAD_MUTEX_INITIALIZER;
   19 
   20 
   21 void common_init(void){
   22     common_logfd[0] = fileno(stderr);
   23     common_logfd[1] = -1;
   24 }
   25 
   26 int common_set_smbnetfs_debug_level(int level){
   27     if ((level < 0) || (level > 10)) return 0;
   28     DPRINTF(8, "level=%d\n", level);
   29     g_atomic_int_set(&common_debug_level, level);
   30     return 1;
   31 }
   32 
   33 int common_set_log_file(const char *logfile){
   34     DPRINTF(7, "logfile=%s\n", logfile);
   35 
   36     pthread_mutex_lock(&m_common);
   37     if ( ! ((logfile != NULL) && (strcmp(common_logfile, logfile) == 0))){
   38     if (common_logfd[1] != -1){
   39         close(common_logfd[1]);
   40         memset(common_logfile, 0, sizeof(common_logfile));
   41         common_logfd[1] = -1;
   42     }
   43 
   44     if (logfile != NULL)
   45         strncpy(common_logfile, logfile, sizeof(common_logfile) - 1);
   46 
   47     if (*common_logfile != '\0'){
   48         common_logfd[1] = open(common_logfile, O_WRONLY | O_APPEND | O_CREAT, 0644);
   49         if (common_logfd[1] == -1){
   50         memset(common_logfile, 0, sizeof(common_logfile));
   51         /* actually we get here if strcmp(common_logfile, logfile) != 0, *
   52          * so we can use variable logfile instead of common_logfile      */
   53         pthread_mutex_unlock(&m_common);
   54         DPRINTF(0, "Can't open logfile '%s', error : %s.\n", logfile, strerror(errno));
   55         return 0;
   56         }
   57     }
   58     }
   59     pthread_mutex_unlock(&m_common);
   60     return 1;
   61 }
   62 
   63 void common_debug_print(const char *fmt, ...){
   64     int     i;
   65     va_list ap;
   66 
   67     pthread_mutex_lock(&m_common);
   68     for(i = 0; i < 2; i++){
   69     if (common_logfd[i] == -1) continue;
   70     va_start(ap, fmt);
   71     vdprintf(common_logfd[i], fmt, ap);
   72     va_end(ap);
   73     }
   74     pthread_mutex_unlock(&m_common);
   75 }
   76 
   77 void common_print_backtrace(void){
   78     int         i;
   79   #ifdef HAVE_BACKTRACE
   80     void        *array[200];
   81     size_t      size;
   82 
   83     size = backtrace(array, sizeof(array) / sizeof(array[0]));
   84   #endif /* HAVE_BACKTRACE */
   85     for(i = 0; i < 2; i++){
   86     if (common_logfd[i] == -1) continue;
   87     dprintf(common_logfd[i], "%d->%s: dumping ...\n", getpid(), __FUNCTION__);
   88       #ifdef HAVE_BACKTRACE
   89     backtrace_symbols_fd(array, size, common_logfd[i]);
   90       #endif /* HAVE_BACKTRACE */
   91     }
   92 }