Skip to content

Instantly share code, notes, and snippets.

@vgonisanz
Last active November 9, 2021 07:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vgonisanz/b11b7b66277c746a1018 to your computer and use it in GitHub Desktop.
Save vgonisanz/b11b7b66277c746a1018 to your computer and use it in GitHub Desktop.
Simple header to print logs in Android/iOS,Windows/Linux/Mac
#pragma once
// Usage:
// VGONI_DEBUG_LOGS 1 = Print logs, if 0 won't.
// LOGD = Debug, LOGI = Info, LOGE = Error, LOGW = Warning, LOGV = Verbose
// 1º Include this header in the cpp to use logs
// 2º Add in top of the file: #define LOG_TAG "CppNameOrID"
// 3º LOGD("Your message: %s in position %d", variable.c_str(), myInt);
//
// Example In myFile.cpp:
// #include "logger.h"
// #define LOG_TAG "myFile"
// ...
// LOGI("Starting LOGS...");
////////////////////////////////////////////////////////////////////////////////////
#define VGONI_DEBUG_LOGS 1
#ifdef VGONI_DEBUG_LOGS
// YES LOGS
# ifdef ANDROID
// LOGS ANDROID
# include <android/log.h>
# define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,__VA_ARGS__)
# define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG,__VA_ARGS__)
# define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOG_TAG,__VA_ARGS__)
# define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOG_TAG,__VA_ARGS__)
# define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOG_TAG,__VA_ARGS__)
# else
// LOGS NO ANDROID (fprintf)
#include <stdio.h>
#include <time.h>
// no works propertly multithreading, if need use a library (Ex. easylogging)!!!
static char buff[100];
static struct tm *sTm;
static time_t now;
# define PRINT_TIME {now = time(0); sTm = localtime(&now); strftime(buff, sizeof(buff), "[%Y-%m-%d %H:%M:%M]", sTm); fprintf(stderr, "%s ", buff); }
# define LOGV(...) {PRINT_TIME fprintf(stderr, "[V][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");}
# define LOGD(...) {PRINT_TIME fprintf(stderr, "[D][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");}
# define LOGI(...) {PRINT_TIME fprintf(stderr, "[I][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");}
# define LOGW(...) {PRINT_TIME fprintf(stderr, "[W][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");}
# define LOGE(...) {PRINT_TIME fprintf(stderr, "[E][%s] ", LOG_TAG); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n");}
# endif // ANDROID
#else
// NO LOGS
# define LOGV(...)
# define LOGD(...)
# define LOGI(...)
# define LOGW(...)
# define LOGE(...)
#endif // VGONI_DEBUG_LOGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment