Skip to content

Instantly share code, notes, and snippets.

@rajephon
Last active November 27, 2019 05:54
Show Gist options
  • Save rajephon/41509c1f700bf824ecc70745949a3f5a to your computer and use it in GitHub Desktop.
Save rajephon/41509c1f700bf824ecc70745949a3f5a to your computer and use it in GitHub Desktop.
Print log with function name in android ndk.
#ifndef TEST_ANDROID_LOG_H
#define TEST_ANDROID_LOG_H
#include <android/log.h>
#define LOG_TAG "NDK_TEST"
#define LOGUNK(...) Log::unk( __func__ , Log::toString(__VA_ARGS__))
#define LOGDEF(...) Log::def( __func__ , Log::toString(__VA_ARGS__))
#define LOGV(...) Log::v(__func__ , Log::toString(__VA_ARGS__))
#define LOGD(...) Log::d(__func__ , Log::toString(__VA_ARGS__))
#define LOGI(...) Log::i(__func__ , Log::toString(__VA_ARGS__))
#define LOGW(...) Log::w(__func__ , Log::toString(__VA_ARGS__))
#define LOGE(...) Log::e(__func__ , Log::toString(__VA_ARGS__))
#define LOGF(...) Log::f(__func__ , Log::toString(__VA_ARGS__))
#define LOGS(...) Log::g(ANDROID_LOG_UNKNOWN_func__ , Log::toString(__VA_ARGS__))
class Log {
public:
static void unk(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_UNKNOWN, function, msg); }
static void def(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_DEFAULT, function, msg); }
static void v(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_VERBOSE, function, msg); }
static void d(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_DEBUG, function, msg); }
static void i(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_INFO, function, msg); }
static void w(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_WARN, function, msg); }
static void e(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_ERROR, function, msg); }
static void f(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_FATAL, function, msg); }
static void s(const char *function, const std::string &msg) { Log::print(ANDROID_LOG_SILENT, function, msg); }
static std::string toString(const char *fmt, ...) {
char *ret;
va_list ap;
va_start(ap, fmt);
vasprintf(&ret, fmt, ap);
va_end(ap);
std::string str(ret);
free(ret);
return str;
}
private:
static void print(android_LogPriority priority, const char *function, const std::string &msg) {
std::string pack = std::string(function) + "::" + msg;
__android_log_print(priority, LOG_TAG, "%s", pack.c_str());
}
};
#endif //TEST_ANDROID_LOG_H
@rajephon
Copy link
Author

rajephon commented Feb 9, 2018

NDK에서 로그를 출력할 때 함수 로그가 호출된 함수의 이름과 같이 출력합니다.
include "Log.h"로 추가하고, LOGD("Hello World! %d", 123); 과 같은 방법으로 사용합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment