Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdarg.h>
- #include <string.h>
- #include <time.h>
- static const char *chaTypes[] = { "SYSTEM1", "SYSTEM2", "SYSTEM3", "SYSTEM4" };
- void writeLogString(const char *str, int len)
- {
- }
- void my_vlogf(int id, int severity, const char *format, va_list args)
- {
- //char buffer[1024];
- //int len = vsnprintf(buffer, sizeof(buffer), format, args);
- fwrite(chaTypes[id], sizeof(char), strlen(chaTypes[id]), stdout);
- fwrite(" ", sizeof(char), 1, stdout);
- //fwrite(buffer, sizeof(char), len, stdout);
- vfprintf(stdout, format, args);
- }
- void my_logf(int id, int severity, const char *format, ...)
- {
- va_list args;
- va_start(args, format);
- my_vlogf(id, severity, format, args);
- va_end(args);
- }
- void mem_logf(int id, int severity, const char *format, ...)
- {
- const char *prefix = chaTypes[id];
- size_t len1 = strlen(prefix);
- size_t len2 = strlen(format) + 1;
- char newFormat[len1 + len2]; // Here be the allocation
- char buffer[1024];
- memcpy(newFormat, prefix, len1);
- memcpy(newFormat + len1, format, len2);
- va_list args;
- va_start(args, format);
- int len = vsnprintf(buffer, sizeof(buffer), newFormat, args);
- va_end(args);
- /*
- * Not sure you actually addressed what the original poster was after,
- * here's your second call to printf.... :/
- *
- * The printf(buffer) is what he had in his example, which actually generates a warning on my compiler
- * considering its unsafe. There's no garantee that buffer doesn't have something that looks like a
- * format string in it! :/
- */
- //printf(buffer);
- //printf("%s", buffer); // Safer, but second call to printf()!
- // This yeilds better performance than the above:
- fwrite(buffer, sizeof(char), len, stdout);
- }
- void null_logf(int id, int severity, const char *format, ...)
- {
- // Here we pretend this log level isn't even enabled.
- if (severity < 10)
- {
- /*
- * This is where you would save a lot of time....
- * Clever use of macros can even save you a stack push!
- */
- return;
- }
- const char *prefix = chaTypes[id];
- size_t len1 = strlen(prefix);
- size_t len2 = strlen(format) + 1;
- char newFormat[len1 + len2]; // Here be the allocation
- char buffer[1024];
- memcpy(newFormat, prefix, len1);
- memcpy(newFormat + len1, format, len2);
- va_list args;
- va_start(args, format);
- int len = vsnprintf(buffer, sizeof(buffer), newFormat, args);
- va_end(args);
- fwrite(buffer, sizeof(char), len, stdout);
- }
- int main(void)
- {
- int i = 0;
- time_t t = time(NULL);
- time_t n;
- do
- {
- n = time(NULL);
- } while (n == t);
- i = 0;
- t = time(NULL);
- do
- {
- mem_logf(1, 0, "number = %d, string = %s", 1, "hi");
- ++i;
- n = time(NULL);
- } while (n == t);
- fprintf(stderr, "Ran mem_logf() %d time(s) in 1 second\r\n", i);
- i = 0;
- t = time(NULL);
- do
- {
- my_logf(1, 0, "number = %d, string = %s", 1, "hi");
- ++i;
- n = time(NULL);
- } while (n == t);
- fprintf(stderr, "Ran my_logf() %d time(s) in 1 second\r\n", i);
- i = 0;
- t = time(NULL);
- do
- {
- null_logf(1, 0, "number = %d, string = %s", 1, "hi");
- ++i;
- n = time(NULL);
- } while (n == t);
- fprintf(stderr, "Ran null_logf() %d time(s) in 1 second\r\n", i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement