source: trunk/client/src/debug.c@ 958

Last change on this file since 958 was 957, checked in by Silvan Scherrer, 9 years ago

samba client: adjust copyright

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