| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/nfs4/logging.cc |
| Date: | 2026-10-18 02:40:11 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 23 | 36 | 63.9% |
| Branches: | 10 | 26 | 38.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "nfs4/logging.h" | ||
| 6 | |||
| 7 | #include <sys/time.h> | ||
| 8 | #include <syslog.h> | ||
| 9 | |||
| 10 | #include <cstdarg> | ||
| 11 | #include <cstdio> | ||
| 12 | #include <cstdlib> | ||
| 13 | |||
| 14 | namespace nfs4 { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | 13 | FILE *DebugStream() { | |
| 19 | 13 | const char *debug = getenv("CVMFS_NFS4_DEBUG"); | |
| 20 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if (debug == NULL) |
| 21 | 13 | return NULL; | |
| 22 | // A path redirects the trace, e.g. for a daemon without stderr | ||
| 23 | ✗ | if (debug[0] == '/') { | |
| 24 | ✗ | FILE *f = fopen(debug, "a"); | |
| 25 | ✗ | if (f) { | |
| 26 | ✗ | setvbuf(f, NULL, _IOLBF, 0); | |
| 27 | ✗ | return f; | |
| 28 | } | ||
| 29 | } | ||
| 30 | ✗ | return stderr; | |
| 31 | } | ||
| 32 | |||
| 33 | 3796 | void DefaultSink(LogLevel level, const char *message) { | |
| 34 |
4/8✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3783 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
|
3796 | static FILE *debug = DebugStream(); |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3796 times.
|
3796 | if (debug) { |
| 36 | struct timeval now; | ||
| 37 | ✗ | gettimeofday(&now, NULL); | |
| 38 | ✗ | fprintf(debug, "%ld.%03d (nfs4) %s\n", static_cast<long>(now.tv_sec), // NOLINT | |
| 39 | ✗ | static_cast<int>(now.tv_usec / 1000), message); | |
| 40 | } | ||
| 41 |
3/4✓ Branch 0 taken 26 times.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3757 times.
|
3796 | switch (level) { |
| 42 | 26 | case kLogLevelInfo: | |
| 43 | 26 | syslog(LOG_INFO, "(nfs4) %s", message); | |
| 44 | 26 | break; | |
| 45 | 13 | case kLogLevelWarn: | |
| 46 | 13 | syslog(LOG_WARNING, "(nfs4) %s", message); | |
| 47 | 13 | break; | |
| 48 | ✗ | case kLogLevelError: | |
| 49 | ✗ | syslog(LOG_ERR, "(nfs4) %s", message); | |
| 50 | ✗ | break; | |
| 51 | 3757 | default: | |
| 52 | 3757 | break; | |
| 53 | } | ||
| 54 | 3796 | } | |
| 55 | |||
| 56 | LogSink g_sink = DefaultSink; | ||
| 57 | |||
| 58 | } // anonymous namespace | ||
| 59 | |||
| 60 | ✗ | void SetLogSink(LogSink sink) { g_sink = sink ? sink : DefaultSink; } | |
| 61 | |||
| 62 | 3796 | void Log(LogLevel level, const char *format, ...) { | |
| 63 | char buf[1024]; | ||
| 64 | va_list args; | ||
| 65 | 3796 | va_start(args, format); | |
| 66 | 3796 | vsnprintf(buf, sizeof(buf), format, args); | |
| 67 | 3796 | va_end(args); | |
| 68 |
1/2✓ Branch 1 taken 3796 times.
✗ Branch 2 not taken.
|
3796 | g_sink(level, buf); |
| 69 | 3796 | } | |
| 70 | |||
| 71 | } // namespace nfs4 | ||
| 72 |