According to the question, i tried to do it as following:
#include <android/log.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#define TAG "mylog"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
uint64_t value = 9999999999;
LOGD("test print uint64_t: %" PRIu64 "\n", value);
However, i got the errors as following:
error: expected ')' before 'PRIu64'
LOGD("test print uint64_t: %" PRIu64 "\n", value);
How to print the uint64_t in Android?
Related
I add some code log at below in android automotive10 source code
diff --git a/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp b/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
index ad452434af..78ee4f80f6 100755
--- a/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
+++ b/frameworks/av/services/audiopolicy/service/AudioPolicyService.cpp
## -16,7 +16,10 ##
#define LOG_TAG "AudioPolicyService"
//#define LOG_NDEBUG 0
-
+//I add
+#include <android/log.h>
+#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
+// end
#include "Configuration.h"
#undef __STRICT_ANSI__
#define __STDINT_LIMITS
## -65,6 +68,10 ## AudioPolicyService::AudioPolicyService()
void AudioPolicyService::onFirstRef()
{
+//I add
+ printf("------------liwx:AudioPolicyService::onFirstRef");
+ char * log =(char*) "liwx_tag";
+ LOGI("------------liwx AudioPolicyService::onFirstRef,%s\n",log);
{
Mutex::Autolock _l(mLock);
I knew some C++ log code can't capture Android, so I add .H file, but it was unuseless
someone can help me, thanks
I want to check the value of the variable in JNI C NDK android, Can anybody help? I need similar to
System.out.println(""+values);
in android.
Define this at the top of the class and try to use like this
#define LOG_TAG "ProjectName"
#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__)
#define LOG_ASSERT(_cond, ...) if (!_cond) __android_log_assert("conditional", LOG_TAG, __VA_ARGS__)
Use like this it will helps you :)
LOGV("Hello World Sample...!");
I am trying to port this logging statement to work, so it will run on linux and android my #define'ing it:
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
I have cross-compiled my app to run on both Linux and Android. However, as linux doesn't have the equivalent I have tried to do my own.
/** ANDROID */
#if defined(__ANDROID__)
#include <android/log.h>
#define LOG_ERROR ANDROID_LOG_ERROR
#define LOG(PRIORITY, fmt, ...) __android_log_print(ANDROID_LOG_UNKNOWN, LOG_TAG, fmt, ##__VA_ARGS__)
/** LINUX */
#elif defined(linux) || defined(__linux) || defined(__linux__)
#define LOG_ERROR LINUX_LOG_ERROR
#define LOG(PRIORITY, fmt, ...) printf(PRIORITY fmt, ##__VA_ARGS__)
#endif
And then using it like this when running under linux
LOG(LOG_ERROR, "Testing loggging [ %d ]", test);
Is there a better way to do this?
Many thanks for any suggestions,
I managed to solve it this way. Here is the complete solution.
This would be in a header file
typedef enum levels_tag levels_e;
enum levels_tag {
LOG_UNKNOWN = 0,
LOG_DEFAULT,
LOG_VERBOSE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
LOG_SILENT,
LOG_LAST
};
/* ANDRIOD IMPLEMENTATION */
#if defined( __ARM_EABI__)
#include <android/log.h>
levels_e levels[LOG_LAST] = {LOG_UNKNOWN,
LOG_DEFAULT,
LOG_VERBOSE,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL,
LOG_SILENT};
#define LOG_TAG "MODULE_LOG_SIP"
#define LOGGING(PRIORITY, fmt, ...) __android_log_print(levels[PRIORITY], LOG_TAG, fmt, ##__VA_ARGS__)
/* LINUX IMPLEMENTATION */
#elif defined(linux) || defined(__linux) || defined(__linux__)
char *priority_levels[] = {"UNKNOWN",
"DEFAULT",
"VERBOSE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL",
"SILENT",
NULL };
#define LOGGING(PRIORITY, fmt, ...) \
do { \
char *priority = priority_levels[PRIORITY]; \
printf("%s/%s:%d [%s] " fmt " \n", __FILE__, __func__, __LINE__, priority, ##__VA_ARGS__); \
} while(0)
#endif
#define LOG(PRIORITY, fmt, ...) LOGGING(PRIORITY, fmt, ##__VA_ARGS__)
And in your source files you would just call the LOG macro like this:
LOG(LOG_FATAL, "Failed to create and initialize application instance [ %d ]", errno);
Now if you were to compile on linux it would use the printf statement. And if you were to compile on Android it would use the __android_log_print statement. Both would produce the same formatted output.
Hope this helps someone else.
You can also use family of syslog() functions to make output to system log instead of stdout/stderr (see syslog.h):
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
Logs can be viewed in /var/log (file depends on options of syslog, by default in many systems logs are in the file "messages"). Sometimes this way is a better than stdout/stderr, but you have to use openlog/closelog. Unfortunately, syslog's priorities are different comparing to android priorities and thus it doesn't allow simple macro definition with transparent priorities.Syslog priorities:
#define LOG_EMERG 0 /* system is unusable */
#define LOG_ALERT 1 /* action must be taken immediately */
#define LOG_CRIT 2 /* critical conditions */
#define LOG_ERR 3 /* error conditions */
#define LOG_WARNING 4 /* warning conditions */
#define LOG_NOTICE 5 /* normal but significant condition */
#define LOG_INFO 6 /* informational */
#define LOG_DEBUG 7 /* debug-level messages */
Priorities of android log:
/*
* Android log priority values, in ascending priority order.
*/
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority;
I set up logging with C++ in Android NDK.
I can print a message to logcat like this:
__android_log_write(ANDROID_LOG_INFO, "tag here", "message here");
Now let's say I have an integer called testint. How can I print the value of this int?
Something like this prints the address, but I want the value. I haven't found anything in C++ on how to do this. Thanks for any help!
__android_log_print(ANDROID_LOG_INFO, "sometag", "%p", *test);
Here's the most concise way I've seen:
#include <android/log.h>
#define LOG_TAG "someTag"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,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__)
...
// Now you can log very simply like this:
int foo = 42;
LOGD( "This is a number from JNI: %d", foo );
Also, make sure you link to the log library in your Android.mk:
LOCAL_LDLIBS := -llog
You could use __android_log_print which uses a sprintf-like syntax that formats your data into a string.
__android_log_print(ANDROID_LOG_INFO, "sometag", "test int = %d", testInt);
Take advantage of the variadic log print function you have available. For my own code, I provide a LogInfo() function to make it simple. Of course there are several options available to you here.
void LogInfo(const char *sTag, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
__android_log_vprint(ANDROID_LOG_INFO, sTag, fmt, ap);
va_end(ap);
}
__android_log_print() takes a format string and a variable argument list. The format specifier you're looking for to print out a signed integer is "%d". So something like this is what you want:
int foo = 42;
__android_log_print(ANDROID_LOG_INFO, "SomeTag", "foo is %d", foo);
For more information on format strings, you can see the sprintf manual.
Original code that prints to stderr:
extern "C" {
/* error: output error message */
void Error(const int error, char *message, ...)
{
va_list arg;
fflush(stdout);
fflush(stderr);
if (error > 0)
fprintf(stderr, "\nError: ");
else
fprintf(stderr, "\nWarning: ");
va_start(arg, message);
vfprintf(stderr, message, arg);
va_end(arg);
fflush(stderr);
if (error > 0)
exit(error);
}
void main(){
Error(0,"Problem %s in file", "sometext");
}
}//extern "C"
I modified my code to look like that. It should print to logcat.
extern "C" {
#include <android/log.h>
#include <jni.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
/* error: output error message */
void Error(const int error, char *message, ...)
{
va_list arg;
va_start(arg, message);
if (error > 0)
__android_log_print(ANDROID_LOG_ERROR, "HTS_API", message, arg);
else
__android_log_print(ANDROID_LOG_WARN, "HTS_API", message, arg);
va_end(arg);
if (error > 0)
exit(error);
}
void main(){
Error(0,"Problem %s in file", "sometext");
}
}//extern "C"
Problem is that my code outputs: 'Problem |�;A.|�;A. in file'
Calling the logger function directly, I will get the expected output.
__android_log_print(ANDROID_LOG_WARN, "HTS_API","Problem %s in file", "sometext");
The expected output is: 'Problem sometext in file'
What am I doing wrong?
__android_log_print does not accept a va_list as a parameter. It accepts a variable parameter list.
It looks like in the newest NDK they've added
int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap);
which is what you want. Formerly you'd have to fix this problem by either re-implementing Error as a macro with variable argument list, or using vsprintf to format the error message in a buffer and then say
__android_log_print(ANDROID_LOG_WARN, "HTS_API", "%s", buf);