2121
2222using namespace oboe ;
2323
24- static char buffer[256 ];
25-
26- // Tracing functions
27- static void *(*ATrace_beginSection)(const char *sectionName);
28-
29- static void *(*ATrace_endSection)();
30-
31- static void *(*ATrace_setCounter)(const char *counterName, int64_t counterValue);
32-
33- static bool *(*ATrace_isEnabled)(void );
34-
3524typedef void *(*fp_ATrace_beginSection)(const char *sectionName);
3625
3726typedef void *(*fp_ATrace_endSection)();
@@ -40,65 +29,65 @@ typedef void *(*fp_ATrace_setCounter)(const char *counterName, int64_t counterVa
4029
4130typedef bool *(*fp_ATrace_isEnabled)(void );
4231
43- bool Trace::mIsTracingEnabled = false ;
44- bool Trace:: mIsSetCounterSupported = false ;
45- bool Trace:: mHasErrorBeenShown = false ;
32+ bool Trace::isEnabled () const {
33+ return ATrace_isEnabled != nullptr && ATrace_isEnabled () ;
34+ }
4635
47- void Trace::beginSection (const char *format, ...){
48- if (mIsTracingEnabled ) {
49- va_list va;
50- va_start (va, format);
51- vsprintf (buffer, format, va);
52- ATrace_beginSection (buffer);
53- va_end (va);
54- } else if (!mHasErrorBeenShown ) {
55- LOGE (" Tracing is either not initialized (call Trace::initialize()) "
56- " or not supported on this device" );
57- mHasErrorBeenShown = true ;
58- }
36+ void Trace::beginSection (const char *format, ...) {
37+ char buffer[256 ];
38+ va_list va;
39+ va_start (va, format);
40+ vsprintf (buffer, format, va);
41+ ATrace_beginSection (buffer);
42+ va_end (va);
5943}
6044
61- void Trace::endSection () {
62- if (mIsTracingEnabled ) {
63- ATrace_endSection ();
64- }
45+ void Trace::endSection () const {
46+ ATrace_endSection ();
6547}
6648
67- void Trace::setCounter (const char *counterName, int64_t counterValue) {
68- if (mIsSetCounterSupported ) {
69- ATrace_setCounter (counterName, counterValue);
70- }
49+ void Trace::setCounter (const char *counterName, int64_t counterValue) const {
50+ ATrace_setCounter (counterName, counterValue);
7151}
7252
73- void Trace::initialize () {
74- // LOGE("Trace::initialize");
53+ Trace::Trace () {
7554 // Using dlsym allows us to use tracing on API 21+ without needing android/trace.h which wasn't
7655 // published until API 23
7756 void *lib = dlopen (" libandroid.so" , RTLD_NOW | RTLD_LOCAL);
57+ LOGD (" Trace(): dlopen(%s) returned %p" , " libandroid.so" , lib);
7858 if (lib == nullptr ) {
79- LOGE (" Could not open libandroid.so to dynamically load tracing symbols" );
59+ LOGE (" Trace() could not open libandroid.so to dynamically load tracing symbols" );
8060 } else {
8161 ATrace_beginSection =
82- reinterpret_cast <fp_ATrace_beginSection >(
62+ reinterpret_cast <fp_ATrace_beginSection>(
8363 dlsym (lib, " ATrace_beginSection" ));
64+ if (ATrace_beginSection == nullptr ) {
65+ LOGE (" Trace::beginSection() not supported" );
66+ return ;
67+ }
68+
8469 ATrace_endSection =
85- reinterpret_cast <fp_ATrace_endSection >(
70+ reinterpret_cast <fp_ATrace_endSection>(
8671 dlsym (lib, " ATrace_endSection" ));
72+ if (ATrace_endSection == nullptr ) {
73+ LOGE (" Trace::endSection() not supported" );
74+ return ;
75+ }
76+
8777 ATrace_setCounter =
88- reinterpret_cast <fp_ATrace_setCounter >(
78+ reinterpret_cast <fp_ATrace_setCounter>(
8979 dlsym (lib, " ATrace_setCounter" ));
80+ if (ATrace_setCounter == nullptr ) {
81+ LOGE (" Trace::setCounter() not supported" );
82+ return ;
83+ }
84+
85+ // If any of the previous functions are null then ATrace_isEnabled will be null.
9086 ATrace_isEnabled =
91- reinterpret_cast <fp_ATrace_isEnabled >(
87+ reinterpret_cast <fp_ATrace_isEnabled>(
9288 dlsym (lib, " ATrace_isEnabled" ));
93-
94- if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr
95- && ATrace_isEnabled != nullptr && ATrace_isEnabled ()) {
96- mIsTracingEnabled = true ;
97- if (ATrace_setCounter != nullptr ) {
98- mIsSetCounterSupported = true ;
99- } else {
100- LOGE (" setCounter not supported" );
101- }
89+ if (ATrace_isEnabled == nullptr ) {
90+ LOGE (" Trace::isEnabled() not supported" );
10291 }
10392 }
10493}
0 commit comments