Repository: gabime/spdlog
File: include/spdlog/sinks/android_sink.h (C/C++)

Code before:
protected:
    void _sink_it(const details::log_msg& msg) override
    {
        const android_LogPriority priority = convert_to_android(msg.level);
        const int expected_size = msg.formatted.size();
        const int size = __android_log_write(
            priority, _tag.c_str(), msg.formatted.c_str()
        );
        if (size > expected_size)
        {
            // Will write a little bit more than original message
        }
        else
        {
            throw spdlog_ex("Send to Android logcat failed");
        }
    }

private:
    static android_LogPriority convert_to_android(spdlog::level::level_enum level)
    {
        switch(level)
        {
            case spdlog::level::trace: return ANDROID_LOG_VERBOSE;
            case spdlog::level::debug: return ANDROID_LOG_DEBUG;
            case spdlog::level::info: return ANDROID_LOG_INFO;
            case spdlog::level::notice: return ANDROID_LOG_INFO;
            case spdlog::level::warn: return ANDROID_LOG_WARN;
            case spdlog::level::err: return ANDROID_LOG_ERROR;
            case spdlog::level::critical: return ANDROID_LOG_FATAL;
            case spdlog::level::alert: return ANDROID_LOG_FATAL;
            case spdlog::level::emerg: return ANDROID_LOG_FATAL;
            case spdlog::level::off: throw spdlog_ex("Unexpected off");
            default: throw spdlog_ex("Incorrect level value");
        }
    }

    std::string _tag;
};

typedef base_android_sink<std::mutex> android_sink_mt;
typedef base_android_sink<details::null_mutex> android_sink_st;

}
}

#endif


Review comment (suggestion):
> simple lookup instead of recalculating over and over again

`simple lookup` mean loading value from memory and `recalculating over and over again` mean quite trivial arithmetic on registers. Just imagine if `spdlog::level::FOO` equals to `ANDROID_LOG_FOO`, i.e. this function just do nothing and return `level` argument. Anyway I think both operations take very small time so I doubt we can take any adequate benchmarks here.

> Also maybe if the test used emerg() instead of info() youd see a bigger difference.

nope, still the same

Also you may find interesting this test: https://gist.github.com/ruslo/f0bb18c73f3d08ea544b
As you can see in case of `boo` (with switch) compiler is smart enough to figure out that result is 0x76 = 0x34 + 0x42, but not in case of `foo` (with loading from memory).


Code after:
protected:
    void _sink_it(const details::log_msg& msg) override
    {
        if (msg.level == spdlog::level::off)
        {
            return;
        }
        const android_LogPriority priority = convert_to_android(msg.level);
        const int expected_size = msg.formatted.size();
        const int size = __android_log_write(
            priority, _tag.c_str(), msg.formatted.c_str()
        );
        if (size > expected_size)
        {
            // Will write a little bit more than original message
        }
        else
        {
            throw spdlog_ex("Send to Android logcat failed");
        }
    }

private:
    static android_LogPriority convert_to_android(spdlog::level::level_enum level)
    {
        switch(level)
        {
            case spdlog::level::trace: return ANDROID_LOG_VERBOSE;
            case spdlog::level::debug: return ANDROID_LOG_DEBUG;
            case spdlog::level::info: return ANDROID_LOG_INFO;
            case spdlog::level::notice: return ANDROID_LOG_INFO;
            case spdlog::level::warn: return ANDROID_LOG_WARN;
            case spdlog::level::err: return ANDROID_LOG_ERROR;
            case spdlog::level::critical: return ANDROID_LOG_FATAL;
            case spdlog::level::alert: return ANDROID_LOG_FATAL;
            case spdlog::level::emerg: return ANDROID_LOG_FATAL;
            default: throw spdlog_ex("Incorrect level value");
        }
    }

    std::string _tag;
};

typedef base_android_sink<std::mutex> android_sink_mt;
typedef base_android_sink<details::null_mutex> android_sink_st;

}
}

#endif