diff options
author | Teddy Wing | 2018-11-04 15:16:28 +0100 |
---|---|---|
committer | Teddy Wing | 2018-11-04 15:16:28 +0100 |
commit | 51509e3b212d6aeae8c946beb454e0ed74e048c7 (patch) | |
tree | e2c4398c24fd6615b70c35bc03efcdc74acfd5fd | |
parent | 2a87aeaefda87c34ff0d240e5e03ce398dd21a02 (diff) | |
download | DomeKey-51509e3b212d6aeae8c946beb454e0ed74e048c7.tar.bz2 |
Add `teprintf()` function
A new function to print to stderr prefixed with a timestamp. The idea is
to make the printed line's format match the Rust logger's.
The Rust logger prints timestamps like this:
2018-11-04T03:16:31.938+01:00
`teprintf` does:
2018-11-04T15:02:21+0100
Unfortunately, I wasn't able to reproduce it exactly. It's missing the
milliseconds because I didn't want to deal with using `gettimeofday`,
and the time zone format isn't right because I didn't know how to format
it easily with a colon.
Resources:
Getting the time in C:
https://stackoverflow.com/questions/9596945/how-to-get-appropriate-timestamp-in-c-for-logs#9596994
Variadic functions in C:
https://stackoverflow.com/questions/205529/passing-variable-number-of-arguments-around
Getting milliseconds in C:
https://stackoverflow.com/questions/10192903/time-in-milliseconds#10192994
https://stackoverflow.com/questions/3673226/how-to-print-time-in-format-2009-08-10-181754-811
https://stackoverflow.com/questions/361363/how-to-measure-time-in-milliseconds-using-ansi-c
Concatenating strings in C:
https://stackoverflow.com/questions/308695/how-do-i-concatenate-const-literal-strings-in-c#308712
https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c
-rw-r--r-- | DomeKey.xcodeproj/project.pbxproj | 4 | ||||
-rw-r--r-- | DomeKey/log.c | 26 | ||||
-rw-r--r-- | DomeKey/log.h | 11 |
3 files changed, 41 insertions, 0 deletions
diff --git a/DomeKey.xcodeproj/project.pbxproj b/DomeKey.xcodeproj/project.pbxproj index 3da05f9..110ee5b 100644 --- a/DomeKey.xcodeproj/project.pbxproj +++ b/DomeKey.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ D110C94A2122F1ED0094F963 /* libddhid.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D160C32221197983007D1B50 /* libddhid.a */; }; D11184622125206E00961687 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D11184612125206E00961687 /* AppDelegate.m */; }; D11CD06D217FECD9001FCB97 /* AquaticPrime.c in Sources */ = {isa = PBXBuildFile; fileRef = D11CD067217FECD9001FCB97 /* AquaticPrime.c */; }; + D12B5B76218F31E300C6F7FC /* log.c in Sources */ = {isa = PBXBuildFile; fileRef = D12B5B71218F31E200C6F7FC /* log.c */; }; D131C4D321663B6500801267 /* Mappings.m in Sources */ = {isa = PBXBuildFile; fileRef = D131C4D221663B6500801267 /* Mappings.m */; }; D160C2A12118EF9D007D1B50 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D160C2A02118EF9D007D1B50 /* main.m */; }; D18255B12180018C00ABC1E0 /* LicenseHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = D18255B02180018C00ABC1E0 /* LicenseHandler.m */; }; @@ -77,6 +78,7 @@ D11184612125206E00961687 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; }; D11CD067217FECD9001FCB97 /* AquaticPrime.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = AquaticPrime.c; path = lib/AquaticPrime/Source/CoreFoundation/AquaticPrime.c; sourceTree = SOURCE_ROOT; }; D11CD06C217FECD9001FCB97 /* AquaticPrime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AquaticPrime.h; path = lib/AquaticPrime/Source/CoreFoundation/AquaticPrime.h; sourceTree = SOURCE_ROOT; }; + D12B5B71218F31E200C6F7FC /* log.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = log.c; sourceTree = "<group>"; }; D131C4D121663B6500801267 /* Mappings.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Mappings.h; sourceTree = "<group>"; }; D131C4D221663B6500801267 /* Mappings.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Mappings.m; sourceTree = "<group>"; }; D160C29D2118EF9D007D1B50 /* DomeKey */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = DomeKey; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -141,6 +143,7 @@ D18255B02180018C00ABC1E0 /* LicenseHandler.m */, D1EAA7A221803BB300A0AC35 /* XDG.h */, D1EAA7A321803BB300A0AC35 /* XDG.m */, + D12B5B71218F31E200C6F7FC /* log.c */, ); path = DomeKey; sourceTree = "<group>"; @@ -273,6 +276,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + D12B5B76218F31E300C6F7FC /* log.c in Sources */, D131C4D321663B6500801267 /* Mappings.m in Sources */, D1EAA7A421803BB300A0AC35 /* XDG.m in Sources */, D1C80CA021877E8200011088 /* Sounds.m in Sources */, diff --git a/DomeKey/log.c b/DomeKey/log.c new file mode 100644 index 0000000..f39e2bf --- /dev/null +++ b/DomeKey/log.c @@ -0,0 +1,26 @@ +#include "log.h" + +void teprintf(char const *fmt, ...) { + // Format the current time + time_t raw_time; + size_t buffer_size = 35; + char buffer[buffer_size]; + time(&raw_time); + struct tm *now = localtime(&raw_time); + strftime(buffer, buffer_size, "%Y-%m-%dT%H:%M:%S%z", now); + + // Prefix the time to the format string + char *timestampped_fmt = malloc(strlen(buffer) + strlen(fmt) + 5); + strcpy(timestampped_fmt, buffer); + strcat(timestampped_fmt, " - "); + strcat(timestampped_fmt, fmt); + strcat(timestampped_fmt, "\n"); + + // Print the log message + va_list args; + va_start(args, fmt); + vfprintf(stderr, timestampped_fmt, args); + va_end(args); + + free(timestampped_fmt); +} diff --git a/DomeKey/log.h b/DomeKey/log.h index 7684143..e285f60 100644 --- a/DomeKey/log.h +++ b/DomeKey/log.h @@ -1,10 +1,21 @@ #ifndef LOG_H #define LOG_H +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + #define LogDebug(...) \ if ([[[[NSProcessInfo processInfo] environment] \ objectForKey:@"DOME_KEY_DEBUG"] isEqualToString:@"1"]) { \ NSLog(__VA_ARGS__); \ } + +// Print a log message prefixed with a timestamp to stderr. Format: +// 2018-11-04T14:35:58+0100 - Format string +void teprintf(char const *fmt, ...); + #endif /* LOG_H */ |