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

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

samba client: remove tabs in source and beautify it completely

  • Property svn:eol-style set to native
File size: 4.2 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 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 rc = DosOpenMutexSem(nameMutex, &logMutex);
90
91 return;
92}
93
94void debugDelete()
95{
96 DosCloseMutexSem(logMutex);
97 return;
98}
99
100void debuglocal(int level, const char * fmt, ...)
101{
102 FILE *f=NULL;
103
104 // do we have to log at all
105 if (!do_logging)
106 return;
107
108 // if the sem is created we request it
109 if (logMutex)
110 DosRequestMutexSem(logMutex, (ULONG) SEM_INDEFINITE_WAIT);
111
112 do {
113 struct timeval tv;
114 char buf[80] = {0};
115 va_list args;
116 if (logfile[0])
117 {
118 f = fopen(logfile, "a");
119 if (!f)
120 break;
121 }
122 else
123 f = stdout;
124
125 // in the first log line we write our version of the client
126 if (firstLogLine)
127 {
128 fprintf(f, "Samba client %s build %s based on %s\n", VERSION, BUILD, smbwrp_getVersion());
129 fprintf(f, "This build is maintained by %s\n", VENDOR);
130 firstLogLine = FALSE;
131 }
132
133 gettimeofday(&tv, NULL);
134 strftime(buf,sizeof(buf)-1,"%Y/%m/%d %H:%M:%S", localtime((time_t *)&tv.tv_sec));
135 fprintf(f, "%s.%d: %d %d: ", buf, tv.tv_usec / 10000, level, (long)_gettid());
136 va_start(args, fmt);
137 vfprintf(f, fmt, args);
138 va_end(args);
139 if (logfile)
140 fclose(f);
141
142 } while (0);
143
144 // if the sem is created we release it
145 if (logMutex)
146 DosReleaseMutexSem(logMutex);
147
148 return;
149}
Note: See TracBrowser for help on using the repository browser.