Skip to content

Commit 445a0b5

Browse files
committed
Log to the Android logd service where appropriate
Add logging macros and use them. The implementation of the macros uses the Android logging library if available, otherwise they use the standard error output.
1 parent 359b8aa commit 445a0b5

File tree

6 files changed

+91
-14
lines changed

6 files changed

+91
-14
lines changed

CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.5)
33
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
44
include(VersioningUtils)
55

6+
include(CheckIncludeFile)
7+
68
read_version_header("" "WPE_[A-Z]+_VERSION" "${CMAKE_CURRENT_SOURCE_DIR}/include/wpe/libwpe-version.h")
79
set_project_version(${WPE_MAJOR_VERSION} ${WPE_MINOR_VERSION} ${WPE_MICRO_VERSION})
810
set(WPE_API_VERSION "1.0")
@@ -129,6 +131,22 @@ if (WPE_ENABLE_XKB)
129131
set(WPE_PC_REQUIRES xkbcommon)
130132
endif ()
131133

134+
check_include_file("android/log.h" HAVE_ANDROID_LOG_H)
135+
if (HAVE_ANDROID_LOG_H)
136+
include(CMakePushCheckState)
137+
include(CheckFunctionExists)
138+
139+
cmake_push_check_state(RESET)
140+
list(APPEND CMAKE_REQUIRED_LIBRARIES -llog)
141+
check_function_exists(__android_log_print HAVE_ANDROID_LOG_PRINT)
142+
cmake_pop_check_state()
143+
144+
if (HAVE_ANDROID_LOG_PRINT)
145+
target_compile_definitions(wpe PRIVATE WPE_ENABLE_ANDROID=1)
146+
target_link_libraries(wpe PRIVATE -llog)
147+
endif ()
148+
endif ()
149+
132150
install(
133151
TARGETS wpe
134152
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}

meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ if not cc.has_function('dlopen')
7676
dependencies += dl_dep
7777
endif
7878

79+
android_log_lib = cc.find_library('log', has_headers: ['android/log.h'], required: false)
80+
if android_log_lib.found()
81+
add_project_arguments('-DWPE_ENABLE_ANDROID=1', language: ['c', 'cpp'])
82+
dependencies += [android_log_lib]
83+
endif
84+
7985
if pkg_cflags.length() > 0
8086
add_project_arguments(pkg_cflags, language: ['c', 'cpp'])
8187
endif

src/alloc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828

2929
#include <stdio.h>
3030

31+
#include "logging-private.h"
32+
3133
void
3234
wpe_alloc_fail(const char* file, unsigned line, size_t amount)
3335
{
34-
fprintf(stderr, "%s:%u: failed to allocate %zu bytes\n", file, line, amount);
35-
fflush(stderr);
36+
wpe_log_fatal("%s:%u: failed to allocate %zu bytes", file, line, amount);
3637
abort();
3738
}

src/loader-static.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
#include "loader-private.h"
2828

29-
#include <stdio.h>
3029
#include <stdlib.h>
3130

31+
#include "logging-private.h"
32+
3233
extern struct wpe_loader_interface _wpe_loader_interface;
3334

3435
bool
@@ -51,9 +52,9 @@ void*
5152
wpe_load_object(const char* object_name)
5253
{
5354
if (!_wpe_loader_interface.load_object) {
54-
fprintf(stderr,
55-
"wpe_load_object: failed to load object with name '%s': backend doesn't implement load_object vfunc\n",
56-
object_name);
55+
wpe_log_fatal(
56+
"wpe_load_object: failed to load object with name '%s': backend doesn't implement load_object vfunc",
57+
object_name);
5758
abort();
5859
}
5960
return _wpe_loader_interface.load_object(object_name);

src/loader.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <string.h>
3232

3333
#include "alloc-private.h"
34+
#include "logging-private.h"
3435

3536
static void* s_impl_library = 0;
3637
static struct wpe_loader_interface* s_impl_loader = 0;
@@ -68,7 +69,7 @@ load_impl_library()
6869
#ifdef WPE_BACKEND
6970
s_impl_library = dlopen(WPE_BACKEND, RTLD_NOW);
7071
if (!s_impl_library) {
71-
fprintf(stderr, "wpe: could not load compile-time defined WPE_BACKEND: %s\n", dlerror());
72+
wpe_log_fatal("could not load compile-time defined WPE_BACKEND: %s", dlerror());
7273
abort();
7374
}
7475
#else
@@ -78,7 +79,7 @@ load_impl_library()
7879
if (env_library_name) {
7980
s_impl_library = dlopen(env_library_name, RTLD_NOW);
8081
if (!s_impl_library) {
81-
fprintf(stderr, "wpe: could not load specified WPE_BACKEND_LIBRARY: %s\n", dlerror());
82+
wpe_log_fatal("could not load specified WPE_BACKEND_LIBRARY: %s", dlerror());
8283
abort();
8384
}
8485
wpe_loader_set_impl_library_name(env_library_name);
@@ -88,7 +89,7 @@ load_impl_library()
8889
// Load libWPEBackend-default.so by ... default.
8990
s_impl_library = dlopen("libWPEBackend-default.so", RTLD_NOW);
9091
if (!s_impl_library) {
91-
fprintf(stderr, "wpe: could not load the impl library. Is there any backend installed?: %s\n", dlerror());
92+
wpe_log_fatal("could not load the impl library. Is there any backend installed?: %s", dlerror());
9293
abort();
9394
}
9495
wpe_loader_set_impl_library_name("libWPEBackend-default.so");
@@ -103,21 +104,21 @@ wpe_loader_init(const char* impl_library_name)
103104
{
104105
#ifndef WPE_BACKEND
105106
if (!impl_library_name) {
106-
fprintf(stderr, "wpe_loader_init: invalid implementation library name\n");
107+
wpe_log_fatal("wpe_loader_init: invalid implementation library name");
107108
abort();
108109
}
109110

110111
if (s_impl_library) {
111112
if (!s_impl_library_name || strcmp(s_impl_library_name, impl_library_name) != 0) {
112-
fprintf(stderr, "wpe_loader_init: already initialized\n");
113+
wpe_log_fatal("wpe_loader_init: already initialized");
113114
return false;
114115
}
115116
return true;
116117
}
117118

118119
s_impl_library = dlopen(impl_library_name, RTLD_NOW);
119120
if (!s_impl_library) {
120-
fprintf(stderr, "wpe_loader_init could not load the library '%s': %s\n", impl_library_name, dlerror());
121+
wpe_log_error("wpe_loader_init could not load the library '%s': %s", impl_library_name, dlerror());
121122
return false;
122123
}
123124
wpe_loader_set_impl_library_name(impl_library_name);
@@ -147,15 +148,17 @@ wpe_load_object(const char* object_name)
147148

148149
if (s_impl_loader) {
149150
if (!s_impl_loader->load_object) {
150-
fprintf(stderr, "wpe_load_object: failed to load object with name '%s': backend doesn't implement load_object vfunc\n", object_name);
151+
wpe_log_fatal(
152+
"wpe_load_object: failed to load object with name '%s': backend doesn't implement load_object vfunc",
153+
object_name);
151154
abort();
152155
}
153156
return s_impl_loader->load_object(object_name);
154157
}
155158

156159
void* object = dlsym(s_impl_library, object_name);
157160
if (!object)
158-
fprintf(stderr, "wpe_load_object: failed to load object with name '%s'\n", object_name);
161+
wpe_log_error("wpe_load_object: failed to load object with name '%s'", object_name);
159162

160163
return object;
161164
}

src/logging-private.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2025 Igalia S.L.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
#pragma once
28+
29+
#if defined(WPE_ENABLE_ANDROID) && WPE_ENABLE_ANDROID
30+
#include <android/log.h>
31+
32+
#define wpe_log(level, fmt, ...) __android_log_print(ANDROID_LOG_##level, "libwpe", (fmt), ##__VA_ARGS__)
33+
34+
#else
35+
#include <stdio.h>
36+
37+
#define wpe_log(level, fmt, ...) \
38+
do { \
39+
fprintf(stderr, "libwpe [" #level "]: " fmt "\n", ##__VA_ARGS__); \
40+
fflush(stderr); \
41+
} while (0)
42+
43+
#endif
44+
45+
#define wpe_log_debug(fmt, ...) wpe_log(DEBUG, fmt, ##__VA_ARGS__)
46+
#define wpe_log_warning(fmt, ...) wpe_log(WARN, fmt, ##__VA_ARGS__)
47+
#define wpe_log_error(fmt, ...) wpe_log(ERROR, fmt, ##__VA_ARGS__)
48+
#define wpe_log_fatal(fmt, ...) wpe_log(FATAL, fmt, ##__VA_ARGS__)

0 commit comments

Comments
 (0)