Skip to content

Commit

Permalink
Fixed #569: Add a logging sink for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Oct 11, 2012
1 parent 2465f40 commit 4cae338
Showing 1 changed file with 97 additions and 3 deletions.
100 changes: 97 additions & 3 deletions src/util/logging.cpp
Expand Up @@ -28,6 +28,10 @@
#include <cstddef>
#include <cstdlib>

#if defined(ANDROID) || defined(__ANDROID__)
#include <android/log.h>
#endif

///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace util
{
Expand Down Expand Up @@ -345,6 +349,28 @@ namespace hpx { namespace util
logging_destination dest_;
};

#if defined(ANDROID) || defined(__ANDROID__)
// default log destination for Android
struct android_log : boost::logging::destination::is_generic
{
android_log(char const* tag_)
: tag(tag_)
{}

template<typename MsgType>
void operator()(MsgType const& msg) const
{
__android_log_write(ANDROID_LOG_DEBUG, tag.c_str(), msg.c_str());
}

bool operator==(console const& rhs) const
{
return tag == rhs.tag;
}

std::string tag;
};
#endif

///////////////////////////////////////////////////////////////////////////
// this is required in order to use the logging library
Expand Down Expand Up @@ -373,8 +399,14 @@ namespace hpx { namespace util

if (boost::logging::level::disable_all != lvl)
{
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "android_log" : "console";
agas_logger()->writer().add_destination("android_log", android_log("hpx.agas"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "cerr" : "console";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand Down Expand Up @@ -421,8 +453,14 @@ namespace hpx { namespace util

if (boost::logging::level::disable_all != lvl)
{
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "android_log" : "console";
timing_logger()->writer().add_destination("android_log", android_log("hpx.timing"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "cerr" : "console";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand Down Expand Up @@ -469,8 +507,15 @@ namespace hpx { namespace util
if (!loglevel.empty())
lvl = detail::get_log_level(loglevel, true);

#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "android_log" : "console";
hpx_logger()->writer().add_destination("android_log", android_log("hpx"));
hpx_error_logger()->writer().add_destination("android_log", android_log("hpx"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "cerr" : "console";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand Down Expand Up @@ -512,7 +557,11 @@ namespace hpx { namespace util
hpx_error_logger()->writer().write(logformat, "console");
}
else {
#if defined(ANDROID) || defined(__ANDROID__)
hpx_error_logger()->writer().write(logformat, "android_log");
#else
hpx_error_logger()->writer().write(logformat, "cerr");
#endif
}
hpx_error_logger()->writer().replace_formatter("osthread", shepherd_thread_id());
hpx_error_logger()->writer().replace_formatter("locality", locality_prefix());
Expand Down Expand Up @@ -556,8 +605,14 @@ namespace hpx { namespace util
lvl = detail::get_log_level(loglevel);

if (boost::logging::level::disable_all != lvl) {
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "android_log" : "console";
app_logger()->writer().add_destination("android_log", android_log("hpx.application"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = isconsole ? "cerr" : "console";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand Down Expand Up @@ -603,8 +658,14 @@ namespace hpx { namespace util

if (boost::logging::level::disable_all != lvl)
{
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = "android_log";
agas_console_logger()->writer().add_destination("android_log", android_log("hpx.agas"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = "cerr";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand Down Expand Up @@ -640,8 +701,14 @@ namespace hpx { namespace util

if (boost::logging::level::disable_all != lvl)
{
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = "android_log";
timing_console_logger()->writer().add_destination("android_log", android_log("hpx.timing"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = "cerr";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand Down Expand Up @@ -675,8 +742,14 @@ namespace hpx { namespace util
if (!loglevel.empty())
lvl = detail::get_log_level(loglevel, true);

if (logdest.empty()) // ensure minimal defaults
logdest = "cerr";
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = "android_log";
hpx_console_logger()->writer().add_destination("android_log", android_log("hpx"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = "cerr";
#endif
if (logformat.empty())
logformat = "|";

Expand Down Expand Up @@ -713,8 +786,14 @@ namespace hpx { namespace util

if (boost::logging::level::disable_all != lvl)
{
#if defined(ANDROID) || defined(__ANDROID__)
if (logdest.empty()) // ensure minimal defaults
logdest = "android_log";
app_console_logger()->writer().add_destination("android_log", android_log("hpx.application"));
#else
if (logdest.empty()) // ensure minimal defaults
logdest = "cerr";
#endif
if (logformat.empty())
logformat = "|\\n";

Expand All @@ -723,7 +802,6 @@ namespace hpx { namespace util
app_console_level()->set_enabled(lvl);
}
}

}}

///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -759,7 +837,11 @@ namespace hpx { namespace util { namespace detail
// general console logging
"[hpx.logging.console]",
"level = ${HPX_LOGLEVEL:$[hpx.logging.level]}",
#if defined(ANDROID) || defined(__ANDROID__)
"destination = ${HPX_CONSOLE_LOGDESTINATION:android_log}",
#else
"destination = ${HPX_CONSOLE_LOGDESTINATION:file(hpx.$[system.pid].log)}",
#endif
"format = ${HPX_CONSOLE_LOGFORMAT:|}",

// logging related to timing
Expand All @@ -774,7 +856,11 @@ namespace hpx { namespace util { namespace detail
// console logging related to timing
"[hpx.logging.console.timing]",
"level = ${HPX_TIMING_LOGLEVEL:$[hpx.logging.timing.level]}",
#if defined(ANDROID) || defined(__ANDROID__)
"destination = ${HPX_CONSOLE_TIMING_LOGDESTINATION:android_log}",
#else
"destination = ${HPX_CONSOLE_TIMING_LOGDESTINATION:file(hpx.timing.$[system.pid].log)}",
#endif
"format = ${HPX_CONSOLE_TIMING_LOGFORMAT:|}",

// logging related to AGAS
Expand All @@ -790,7 +876,11 @@ namespace hpx { namespace util { namespace detail
// console logging related to AGAS
"[hpx.logging.console.agas]",
"level = ${HPX_AGAS_LOGLEVEL:$[hpx.logging.agas.level]}",
#if defined(ANDROID) || defined(__ANDROID__)
"destination = ${HPX_CONSOLE_AGAS_LOGDESTINATION:android_log}",
#else
"destination = ${HPX_CONSOLE_AGAS_LOGDESTINATION:file(hpx.agas.$[system.pid].log)}",
#endif
"format = ${HPX_CONSOLE_AGAS_LOGFORMAT:|}",

// logging related to applications
Expand All @@ -805,7 +895,11 @@ namespace hpx { namespace util { namespace detail
// console logging related to applications
"[hpx.logging.console.application]",
"level = ${HPX_APP_LOGLEVEL:$[hpx.logging.application.level]}",
#if defined(ANDROID) || defined(__ANDROID__)
"destination = ${HPX_CONSOLE_APP_LOGDESTINATION:android_log}",
#else
"destination = ${HPX_CONSOLE_APP_LOGDESTINATION:file(hpx.application.$[system.pid].log)}",
#endif
"format = ${HPX_CONSOLE_APP_LOGFORMAT:|}"
;
}
Expand Down

0 comments on commit 4cae338

Please sign in to comment.