Skip to content

Commit

Permalink
sinklib: add android sink
Browse files Browse the repository at this point in the history
  • Loading branch information
iboB committed Jul 1, 2024
1 parent aa16630 commit 0cd5019
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ext/jalog/sinks/AndroidSink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Borislav Stanimirov
// SPDX-License-Identifier: MIT
//
#include "AndroidSink.hpp"

#include <jalog/Entry.hpp>

#include <android/log.h>

namespace jalog::sinks {

namespace {
android_LogPriority jalogToAndroid(jalog::Level level) {
switch (level) {
case jalog::Level::Debug: return ANDROID_LOG_DEBUG;
case jalog::Level::Info: return ANDROID_LOG_INFO;
case jalog::Level::Warning: return ANDROID_LOG_WARN;
case jalog::Level::Error: return ANDROID_LOG_ERROR;
case jalog::Level::Critical: return ANDROID_LOG_FATAL;
default: return ANDROID_LOG_DEFAULT;
}
}
}

void AndroidSink::record(const Entry& entry) {
auto lbl = entry.scope.labelCStr();
auto txt = entry.text;

__android_log_print(jalogToAndroid(entry.level), lbl, "%.*s", int(txt.length()), txt.data());
}

}
17 changes: 17 additions & 0 deletions ext/jalog/sinks/AndroidSink.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Borislav Stanimirov
// SPDX-License-Identifier: MIT
//
#pragma once
#include "API.h"
#include <jalog/Sink.hpp>

namespace jalog::sinks
{

class JALOG_SINKLIB_API AndroidSink final : public Sink
{
public:
virtual void record(const Entry& entry) override;
};

}
6 changes: 6 additions & 0 deletions ext/jalog/sinks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if(WIN32)
WindowsColorSink.hpp
WindowsColorSink.cpp
)
elseif(ANDROID)
target_sources(jalog-sinklib PRIVATE
AndroidSink.hpp
AndroidSink.cpp
)
target_link_libraries(jalog-sinklib PRIVATE log)
endif()

target_include_directories(jalog-sinklib INTERFACE ../..)
Expand Down
15 changes: 15 additions & 0 deletions ext/jalog/sinks/DefaultSink.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Borislav Stanimirov
// SPDX-License-Identifier: MIT
//
#pragma once

#if defined(_WIN32)
#include "WindowsColorSink.hpp"
namespace jalog::sinks { using DefaultSink = WindowsColorSink; }
#elif defined(__ANDROID__)
#include "AndroidSink.hpp"
namespace jalog::sinks { using DefaultSink = AndroidSink; }
#else
#include "AnsiColorSink.hpp"
namespace jalog::sinks { using DefaultSink = AnsiColorSink; }
#endif
10 changes: 10 additions & 0 deletions test/t-core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <jalog/LogStream.hpp>

#include <jalog/Logger.hpp>
#include <jalog/DefaultScope.hpp>

TEST_SUITE_BEGIN("jalog");

Expand Down Expand Up @@ -35,6 +36,15 @@ TEST_CASE("scopes")
CHECK(longname.desc().label() == "0123456789ABCDE");
}

TEST_CASE("default scope")
{
auto& s = jalog::Default_Scope;
auto& sd = s.desc();
CHECK(sd.id() == 0);
CHECK(sd.label().empty());
CHECK(*sd.labelCStr() == 0);
CHECK(sd.userData == -1);
}

struct TestHelper
{
Expand Down

0 comments on commit 0cd5019

Please sign in to comment.