diff --git a/include/spdlog/details/format.cc b/include/spdlog/details/format.cc index 08af20fb..c39c6849 100644 --- a/include/spdlog/details/format.cc +++ b/include/spdlog/details/format.cc @@ -141,7 +141,9 @@ FMT_FUNC int safe_strerror( int error_code, char *&buffer, std::size_t buffer_size) FMT_NOEXCEPT(true) { assert(buffer != 0 && buffer_size != 0); int result = 0; -#ifdef _GNU_SOURCE +#ifdef __ANDROID__ + result = strerror_r(error_code, buffer, buffer_size); +#elif defined(_GNU_SOURCE) char *message = strerror_r(error_code, buffer, buffer_size); // If the buffer is full then the message is probably truncated. if (message == buffer && strlen(buffer) == buffer_size - 1) @@ -1165,4 +1167,4 @@ template int spdlog::details::fmt::internal::CharTraits::format_float( #if _MSC_VER # pragma warning(pop) -#endif \ No newline at end of file +#endif diff --git a/include/spdlog/sinks/android_sink.h b/include/spdlog/sinks/android_sink.h new file mode 100644 index 00000000..690373b7 --- /dev/null +++ b/include/spdlog/sinks/android_sink.h @@ -0,0 +1,77 @@ +/*************************************************************************/ +/* spdlog - an extremely fast and easy to use c++11 logging library. */ +/* Copyright (c) 2015 Francois Coulombe. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#pragma once +#ifdef __ANDROID__ +#include +#include +#include +#include +#include "../details/null_mutex.h" +#include "./base_sink.h" + + +namespace spdlog +{ +namespace sinks +{ +template +class android_sink: public base_sink +{ +public: + explicit android_sink(std::ostream& /*os*/, bool force_flush=false) + ://_ostream(os), + _force_flush(force_flush) {} + android_sink(const android_sink&) = delete; + android_sink& operator=(const android_sink&) = delete; + virtual ~android_sink() = default; + +protected: + virtual void _sink_it(const details::log_msg& msg) override + { + constexpr int LoveLevelMapping[] = + { + ANDROID_LOG_VERBOSE, + ANDROID_LOG_DEBUG, + ANDROID_LOG_INFO, + ANDROID_LOG_INFO, + ANDROID_LOG_WARN, + ANDROID_LOG_ERROR, + ANDROID_LOG_FATAL, + ANDROID_LOG_FATAL, + ANDROID_LOG_FATAL, + ANDROID_LOG_SILENT, + }; + ((void)__android_log_print(LoveLevelMapping[msg.level], msg.logger_name.c_str(), msg.formatted.data())); + } + + //std::ostream& _ostream; + bool _force_flush; +}; + +typedef android_sink android_sink_mt; +typedef android_sink android_sink_st; +} +} +#endif diff --git a/include/spdlog/sinks/stdout_sinks.h b/include/spdlog/sinks/stdout_sinks.h index e944e273..47ed4865 100644 --- a/include/spdlog/sinks/stdout_sinks.h +++ b/include/spdlog/sinks/stdout_sinks.h @@ -26,6 +26,8 @@ #include #include + +#include "./android_sink.h" #include "./ostream_sink.h" #include "../details/null_mutex.h" @@ -35,10 +37,16 @@ namespace sinks { template -class stdout_sink : public ostream_sink +#ifdef __ANDROID__ +# define outstream_sink android_sink +#else +# define outstream_sink ostream_sink +#endif + +class stdout_sink : public outstream_sink { public: - stdout_sink() : ostream_sink(std::cout, true) {} + stdout_sink() : outstream_sink(std::cout, true) {} }; @@ -47,10 +55,10 @@ typedef stdout_sink stdout_sink_mt; template -class stderr_sink : public ostream_sink +class stderr_sink : public outstream_sink { public: - stderr_sink() : ostream_sink(std::cerr, true) {} + stderr_sink() : outstream_sink(std::cerr, true) {} }; typedef stderr_sink stderr_sink_mt;