| 1 | /* | 
|---|
| 2 | Netdrive Samba client plugin | 
|---|
| 3 | logging functions | 
|---|
| 4 | Copyright (C) netlabs.org 2003-2009 | 
|---|
| 5 |  | 
|---|
| 6 | This program is free software; you can redistribute it and/or modify | 
|---|
| 7 | it under the terms of the GNU General Public License as published by | 
|---|
| 8 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 9 | (at your option) any later version. | 
|---|
| 10 |  | 
|---|
| 11 | This program is distributed in the hope that it will be useful, | 
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 14 | GNU General Public License for more details. | 
|---|
| 15 |  | 
|---|
| 16 | You should have received a copy of the GNU General Public License | 
|---|
| 17 | along with this program; if not, write to the Free Software | 
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | #define INCL_DOSERRORS | 
|---|
| 22 | #define INCL_DOS | 
|---|
| 23 | #include <os2emx.h> | 
|---|
| 24 | #include <stdio.h> | 
|---|
| 25 | #include <stdlib.h> | 
|---|
| 26 | #include <stdarg.h> | 
|---|
| 27 | #include <string.h> | 
|---|
| 28 | #include <sys/types.h> | 
|---|
| 29 | #include <sys/time.h> | 
|---|
| 30 | #include <sys/stat.h> | 
|---|
| 31 | #include "nversion.h" | 
|---|
| 32 |  | 
|---|
| 33 | int debuglevel = 9; // we set it to 9, so we get all messages | 
|---|
| 34 | char logfile[_MAX_PATH +1]; | 
|---|
| 35 | char debugfile[_MAX_PATH +1]; | 
|---|
| 36 | char logfilename[] = "log.ndpsmb"; | 
|---|
| 37 | BOOL do_logging; | 
|---|
| 38 | BOOL firstLogLine; | 
|---|
| 39 | HMTX logMutex; | 
|---|
| 40 | char nameMutex[] = "\\SEM32\\NDPSMB"; | 
|---|
| 41 |  | 
|---|
| 42 | int debuglvl(int level) | 
|---|
| 43 | { | 
|---|
| 44 | return (level <= debuglevel) ? 1 : 0; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | BOOL writeLog() | 
|---|
| 48 | { | 
|---|
| 49 | return do_logging; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void debugInit() | 
|---|
| 53 | { | 
|---|
| 54 | *logfile = '\0'; | 
|---|
| 55 | *debugfile = '\0'; | 
|---|
| 56 | do_logging = FALSE; | 
|---|
| 57 | firstLogLine = TRUE; | 
|---|
| 58 | logMutex = NULLHANDLE; | 
|---|
| 59 | struct stat filestat; | 
|---|
| 60 | APIRET rc = NO_ERROR; | 
|---|
| 61 |  | 
|---|
| 62 | // create the debugfile name | 
|---|
| 63 | strncat(debugfile, getenv("ETC"), 2); | 
|---|
| 64 | strncat(debugfile, "\\", sizeof(debugfile) - strlen(debugfile) -1); | 
|---|
| 65 | strncat(debugfile, "ndpsmb.dbg", sizeof(debugfile) - strlen(debugfile) -1); | 
|---|
| 66 |  | 
|---|
| 67 | // is the file around? if not we have no debug | 
|---|
| 68 | if (stat(debugfile, &filestat) !=0) | 
|---|
| 69 | return; | 
|---|
| 70 |  | 
|---|
| 71 | //create the logfile variable | 
|---|
| 72 | char *env = getenv("LOGFILES"); | 
|---|
| 73 | if (env != NULL) | 
|---|
| 74 | { | 
|---|
| 75 | strncat(logfile, env, sizeof(logfile) -1); | 
|---|
| 76 | strncat(logfile, "\\", sizeof(logfile) - strlen(logfile) -1); | 
|---|
| 77 | strncat(logfile, logfilename, sizeof(logfile) - strlen(logfile) -1); | 
|---|
| 78 | } | 
|---|
| 79 | else | 
|---|
| 80 | { | 
|---|
| 81 | strncat(logfile, logfilename, sizeof(logfile) -1); | 
|---|
| 82 | } | 
|---|
| 83 | // set the samba logging stuff | 
|---|
| 84 | do_logging = TRUE; | 
|---|
| 85 |  | 
|---|
| 86 | // now we create a sem, so that logging from different threads works | 
|---|
| 87 | rc = DosCreateMutexSem(nameMutex, &logMutex, 0, FALSE); | 
|---|
| 88 | if (rc == ERROR_DUPLICATE_NAME) | 
|---|
| 89 | { | 
|---|
| 90 | rc = DosOpenMutexSem(nameMutex, &logMutex); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | return; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void debugDelete() | 
|---|
| 97 | { | 
|---|
| 98 | DosCloseMutexSem(logMutex); | 
|---|
| 99 | return; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | void debuglocal(int level, const char * fmt, ...) | 
|---|
| 103 | { | 
|---|
| 104 | FILE *f=NULL; | 
|---|
| 105 |  | 
|---|
| 106 | // do we have to log at all | 
|---|
| 107 | if (!do_logging) | 
|---|
| 108 | { | 
|---|
| 109 | return; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | // if the sem is created we request it | 
|---|
| 113 | if (logMutex) | 
|---|
| 114 | { | 
|---|
| 115 | DosRequestMutexSem(logMutex, (ULONG) SEM_INDEFINITE_WAIT); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | do | 
|---|
| 119 | { | 
|---|
| 120 | struct timeval tv; | 
|---|
| 121 | char buf[80] = {0}; | 
|---|
| 122 | va_list args; | 
|---|
| 123 | if (logfile[0]) | 
|---|
| 124 | { | 
|---|
| 125 | f = fopen(logfile, "a"); | 
|---|
| 126 | if (!f) | 
|---|
| 127 | { | 
|---|
| 128 | break; | 
|---|
| 129 | } | 
|---|
| 130 | } | 
|---|
| 131 | else | 
|---|
| 132 | { | 
|---|
| 133 | f = stdout; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | // in the first log line we write our version of the client | 
|---|
| 137 | if (firstLogLine) | 
|---|
| 138 | { | 
|---|
| 139 | fprintf(f, "Samba client %s build %s based on %s\n", VERSION, BUILD, smbwrp_getVersion()); | 
|---|
| 140 | firstLogLine = FALSE; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | gettimeofday(&tv, NULL); | 
|---|
| 144 | strftime(buf,sizeof(buf)-1,"%Y/%m/%d %H:%M:%S", localtime((time_t *)&tv.tv_sec)); | 
|---|
| 145 | fprintf(f, "%s.%d: %d %d: ", buf, tv.tv_usec / 10000, level, (long)_gettid()); | 
|---|
| 146 | va_start(args, fmt); | 
|---|
| 147 | vfprintf(f, fmt, args); | 
|---|
| 148 | va_end(args); | 
|---|
| 149 | if (logfile) | 
|---|
| 150 | { | 
|---|
| 151 | //                      fflush(f); | 
|---|
| 152 | fclose(f); | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 | while (0); | 
|---|
| 156 |  | 
|---|
| 157 | // if the sem is created we release it | 
|---|
| 158 | if (logMutex) | 
|---|
| 159 | { | 
|---|
| 160 | DosReleaseMutexSem(logMutex); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | return; | 
|---|
| 164 | } | 
|---|