From b3db0e6ee14315d20ca91eff91e348e18dd119e6 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 13 May 2016 11:53:33 +0100 Subject: translate logging.cpp to C --- src/account.cpp | 8 ++++---- src/logging.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/logging.cpp | 51 --------------------------------------------------- src/message.cpp | 6 +++--- src/olm.cpp | 8 +------- src/ratchet.cpp | 24 ++++++++++++------------ src/session.cpp | 30 +++++++++++++++--------------- 7 files changed, 82 insertions(+), 92 deletions(-) create mode 100644 src/logging.c delete mode 100644 src/logging.cpp (limited to 'src') diff --git a/src/account.cpp b/src/account.cpp index 4fca953..d34110a 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -14,7 +14,7 @@ */ #include "olm/account.hh" #include "olm/base64.hh" -#include "olm/logging.hh" +#include "olm/logging.h" #include "olm/pickle.hh" #include "olm/memory.hh" @@ -45,11 +45,11 @@ std::size_t olm::Account::remove_key( if (olm::array_equal(i->key.public_key, public_key.public_key)) { std::uint32_t id = i->id; one_time_keys.erase(i); - olm::logf(olm::LOG_INFO, LOG_CATEGORY, "removed key id %i", id); + olm_logf(OLM_LOG_INFO, LOG_CATEGORY, "removed key id %i", id); return id; } } - olm::logf(olm::LOG_WARNING, LOG_CATEGORY, "Couldn't find key to remove"); + olm_logf(OLM_LOG_WARNING, LOG_CATEGORY, "Couldn't find key to remove"); return std::size_t(-1); } @@ -69,7 +69,7 @@ std::size_t olm::Account::new_account( random += KEY_LENGTH; olm::curve25519_generate_key(random, identity_keys.curve25519_key); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Created new account"); + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Created new account"); return 0; } diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..ce17167 --- /dev/null +++ b/src/logging.c @@ -0,0 +1,47 @@ +/* Copyright 2016 OpenMarket Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "olm/logging.h" + +#include +#include + +static unsigned int log_level = 1; + +void olm_set_log_level(unsigned int level) { + log_level = level; +} + +int olm_log_enabled_for(unsigned int level, const char *category) +{ + return level <= log_level; +} + +void olm_logf(unsigned int level, const char *category, + const char *format, ...) { + if (level > log_level) { + return; + } + + fputs(category, stdout); + fputs(": ", stdout); + + va_list ap; + va_start(ap, format); + vprintf(format, ap); + va_end(ap); + + putchar('\n'); +} diff --git a/src/logging.cpp b/src/logging.cpp deleted file mode 100644 index 4819e1c..0000000 --- a/src/logging.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* Copyright 2016 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm/logging.hh" - -#include -#include - -namespace olm { - -static unsigned int log_level = 1; - -void set_log_level(unsigned int level) { - log_level = level; -} - -bool log_enabled_for(unsigned int level, const char *category) -{ - return level <= log_level; -} - -void logf(unsigned int level, const char *category, - const char *format, ...) { - if (level > log_level) { - return; - } - - fputs(category, stdout); - fputs(": ", stdout); - - va_list ap; - va_start(ap, format); - vprintf(format, ap); - va_end(ap); - - putchar('\n'); -} - -} // namespace olm diff --git a/src/message.cpp b/src/message.cpp index 23ec823..ff03903 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -14,7 +14,7 @@ */ #include "olm/message.hh" -#include "olm/logging.hh" +#include "olm/logging.h" #include "olm/memory.hh" namespace { @@ -239,7 +239,7 @@ void olm::decode_message( } unknown = pos; } - olm::logf(olm::LOG_TRACE, LOG_CATEGORY, + olm_logf(OLM_LOG_TRACE, LOG_CATEGORY, "Decoded message ver=%i ratchet_key=%s chain_idx=%i ciphertext=%s", reader.version, olm::bytes_to_string(reader.ratchet_key, reader.ratchet_key_length).c_str(), @@ -335,7 +335,7 @@ void olm::decode_one_time_key_message( unknown = pos; } - olm::logf(olm::LOG_TRACE, LOG_CATEGORY, + olm_logf(OLM_LOG_TRACE, LOG_CATEGORY, "Decoded pre-key message ver=%i one_time_key[Eb]=%s " "base_key[Ea]=%s identity_key[Ia]=%s message=%s", reader.version, diff --git a/src/olm.cpp b/src/olm.cpp index 6db8724..afbbbc6 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -19,7 +19,7 @@ #include "olm/base64.hh" #include "olm/cipher.hh" #include "olm/memory.hh" -#include "olm/logging.hh" +#include "olm/logging.h" #include #include @@ -819,10 +819,4 @@ size_t olm_ed25519_verify( ); } -void olm_set_log_level( - unsigned int level -) { - olm::set_log_level(level); -} - } diff --git a/src/ratchet.cpp b/src/ratchet.cpp index 03a0478..06912af 100644 --- a/src/ratchet.cpp +++ b/src/ratchet.cpp @@ -17,7 +17,7 @@ #include "olm/memory.hh" #include "olm/cipher.hh" #include "olm/pickle.hh" -#include "olm/logging.hh" +#include "olm/logging.h" #include @@ -79,7 +79,7 @@ static void advance_chain_key( new_chain_key.key ); new_chain_key.index = chain_key.index + 1; - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Derived chain key C(%i,%i)", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Derived chain key C(%i,%i)", chain_index, new_chain_key.index); } @@ -95,7 +95,7 @@ static void create_message_keys( message_key.key ); message_key.index = chain_key.index; - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Created message key with chain key C(%i,%i)", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Created message key with chain key C(%i,%i)", chain_index, message_key.index); } @@ -176,7 +176,7 @@ static std::size_t verify_mac_and_decrypt_for_new_chain( new_chain.ratchet_key, session.kdf_info, new_root_key, new_chain.chain_key ); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Calculated new receiver chain R(%i)", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Calculated new receiver chain R(%i)", chain_index); std::size_t result = verify_mac_and_decrypt_for_existing_chain( @@ -219,7 +219,7 @@ void olm::Ratchet::initialise_as_bob( receiver_chains[0].ratchet_key = their_ratchet_key; chain_index = 0; olm::unset(derived_secrets); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Initialised receiver chain R(0)"); + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Initialised receiver chain R(0)"); } @@ -242,7 +242,7 @@ void olm::Ratchet::initialise_as_alice( sender_chain[0].ratchet_key = our_ratchet_key; chain_index = 0; olm::unset(derived_secrets); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Initialised sender chain R(0)"); + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Initialised sender chain R(0)"); } namespace olm { @@ -450,7 +450,7 @@ std::size_t olm::Ratchet::encrypt( if (sender_chain.empty()) { sender_chain.insert(); olm::curve25519_generate_key(random, sender_chain[0].ratchet_key); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Created new ratchet key T(%i) %s", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Created new ratchet key T(%i) %s", chain_index + 1, sender_chain[0].ratchet_key.to_string().c_str()); create_chain_key( @@ -460,7 +460,7 @@ std::size_t olm::Ratchet::encrypt( kdf_info, root_key, sender_chain[0].chain_key ); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Initialised new sender chain R(%i)", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Initialised new sender chain R(%i)", chain_index + 1); chain_index++; } @@ -491,7 +491,7 @@ std::size_t olm::Ratchet::encrypt( output, output_length ); - olm::logf(olm::LOG_TRACE, LOG_CATEGORY, + olm_logf(OLM_LOG_TRACE, LOG_CATEGORY, "Encoded message ver=%i ratchet_key=%s chain_idx=%i ciphertext=%s", PROTOCOL_VERSION, olm::bytes_to_string(writer.ratchet_key, olm::KEY_LENGTH).c_str(), @@ -525,7 +525,7 @@ std::size_t olm::Ratchet::decrypt( std::uint8_t const * input, std::size_t input_length, std::uint8_t * plaintext, std::size_t max_plaintext_length ) { - olm::logf(olm::LOG_TRACE, LOG_CATEGORY, + olm_logf(OLM_LOG_TRACE, LOG_CATEGORY, "Decrypting message %s", olm::bytes_to_string(input, input_length).c_str()); @@ -580,7 +580,7 @@ std::size_t olm::Ratchet::decrypt( std::size_t result = std::size_t(-1); if (!chain) { - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Sender ratchet key does not match known chain; starting new one"); result = verify_mac_and_decrypt_for_new_chain( *this, reader, plaintext, max_plaintext_length @@ -642,7 +642,7 @@ std::size_t olm::Ratchet::decrypt( olm::unset(sender_chain[0]); sender_chain.erase(sender_chain.begin()); receiver_chain_index = ++chain_index; - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Initialised new receiver chain R(%i)", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Initialised new receiver chain R(%i)", chain_index); } diff --git a/src/session.cpp b/src/session.cpp index 6a9bb7e..816dd26 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -16,7 +16,7 @@ #include "olm/cipher.hh" #include "olm/crypto.hh" #include "olm/account.hh" -#include "olm/logging.hh" +#include "olm/logging.h" #include "olm/memory.hh" #include "olm/message.hh" #include "olm/pickle.hh" @@ -68,7 +68,7 @@ std::size_t olm::Session::new_outbound_session( return std::size_t(-1); } - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Creating new outbound session to receiver identity IB %s, " "receiver ephemeral EB %s", identity_key.to_string().c_str(), one_time_key.to_string().c_str() @@ -76,12 +76,12 @@ std::size_t olm::Session::new_outbound_session( olm::Curve25519KeyPair base_key; olm::curve25519_generate_key(random, base_key); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Created new ephemeral key EA %s", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Created new ephemeral key EA %s", base_key.to_string().c_str()); olm::Curve25519KeyPair ratchet_key; olm::curve25519_generate_key(random + olm::KEY_LENGTH, ratchet_key); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Created new ratchet key T(0) %s", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Created new ratchet key T(0) %s", ratchet_key.to_string().c_str()); olm::Curve25519KeyPair const & alice_identity_key_pair = ( @@ -108,7 +108,7 @@ std::size_t olm::Session::new_outbound_session( olm::unset(ratchet_key); olm::unset(secret); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Initialised outbound session"); + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Initialised outbound session"); return std::size_t(0); } @@ -151,7 +151,7 @@ std::size_t olm::Session::new_inbound_session( their_identity_key->public_key, reader.identity_key, olm::KEY_LENGTH ); if (!same) { - olm::logf(olm::LOG_INFO, LOG_CATEGORY, + olm_logf(OLM_LOG_INFO, LOG_CATEGORY, "Identity key on received message is incorrect " "(expected %s, got %s)", their_identity_key->to_string().c_str(), @@ -167,7 +167,7 @@ std::size_t olm::Session::new_inbound_session( olm::load_array(alice_base_key.public_key, reader.base_key); olm::load_array(bob_one_time_key.public_key, reader.one_time_key); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Creating new inbound session from sender identity IA %s, " "sender ephemeral EA %s, our ephemeral EB %s", alice_identity_key.to_string().c_str(), @@ -189,7 +189,7 @@ std::size_t olm::Session::new_inbound_session( olm::Curve25519PublicKey ratchet_key; olm::load_array(ratchet_key.public_key, message_reader.ratchet_key); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Received ratchet key T(0) %s", ratchet_key.to_string().c_str()); olm::OneTimeKey const * our_one_time_key = local_account.lookup_key( @@ -197,7 +197,7 @@ std::size_t olm::Session::new_inbound_session( ); if (!our_one_time_key) { - olm::logf(olm::LOG_INFO, LOG_CATEGORY, + olm_logf(OLM_LOG_INFO, LOG_CATEGORY, "Session uses unknown ephemeral key %s", bob_one_time_key.to_string().c_str()); last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; @@ -221,7 +221,7 @@ std::size_t olm::Session::new_inbound_session( olm::unset(secret); - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Initialised inbound session"); + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Initialised inbound session"); return std::size_t(0); } @@ -320,7 +320,7 @@ std::size_t olm::Session::encrypt( std::uint8_t const * random, std::size_t random_length, std::uint8_t * message, std::size_t message_length ) { - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Encrypting '%.*s'", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Encrypting '%.*s'", (int)plaintext_length, plaintext); if (message_length < encrypt_message_length(plaintext_length)) { @@ -351,7 +351,7 @@ std::size_t olm::Session::encrypt( message_body = writer.message; - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Encoded pre-key message ver=%i one_time_key[Eb]=%s " "base_key[Ea]=%s identity_key[Ia]=%s", PROTOCOL_VERSION, @@ -373,7 +373,7 @@ std::size_t olm::Session::encrypt( return result; } - olm::logf(olm::LOG_TRACE, LOG_CATEGORY, "Encrypted message %s", + olm_logf(OLM_LOG_TRACE, LOG_CATEGORY, "Encrypted message %s", olm::bytes_to_string(message_body, result).c_str()); return result; @@ -417,7 +417,7 @@ std::size_t olm::Session::decrypt( std::uint8_t const * message, std::size_t message_length, std::uint8_t * plaintext, std::size_t max_plaintext_length ) { - olm::logf(olm::LOG_TRACE, LOG_CATEGORY, "Decrypting %smessage", + olm_logf(OLM_LOG_TRACE, LOG_CATEGORY, "Decrypting %smessage", message_type == olm::MessageType::MESSAGE ? "" : "pre-key "); std::uint8_t const * message_body; @@ -447,7 +447,7 @@ std::size_t olm::Session::decrypt( } received_message = true; - olm::logf(olm::LOG_DEBUG, LOG_CATEGORY, "Decrypted '%.*s'", + olm_logf(OLM_LOG_DEBUG, LOG_CATEGORY, "Decrypted '%.*s'", (int)result, plaintext); return result; } -- cgit v1.2.3