diff options
author | 2021-10-20 21:02:22 +0200 | |
---|---|---|
committer | 2021-10-20 19:02:22 +0000 | |
commit | 02096d079680a2f7125db230bcde872dde63732c (patch) | |
tree | 14064a6a0b029a16f77ad357772546334e841867 /Source/Utils/MsgLogger/MsgLoggerSerialization.H | |
parent | c3f481bca813d834e248d1adeb5b5a02495b9e1a (diff) | |
download | WarpX-02096d079680a2f7125db230bcde872dde63732c.tar.gz WarpX-02096d079680a2f7125db230bcde872dde63732c.tar.zst WarpX-02096d079680a2f7125db230bcde872dde63732c.zip |
⚠️ Add warning logger (#2113)
* improve error msg
* eliminate final dot
* initial layout of the main classes
* progress
* progress with warnings
* print local errors
* progress with warning logger
* improved MsgLogger
* added file to test warnings
* added method to read debug warnings from inputfile
* progress towards collective gather of warnings
* add missing blank line in warning message
* move misplaced file
* refactoring
* fixed bugs
* progress
* fixed bugs
* fixed some bugs
* it finally works!
* add comments in WarpX.H
* add missing comment
* added comments to WarpX.cpp
* add profiler to WarpX.cpp
* expose option to enable 'warn immediately' feature in cmake
* Add comment to main.cpp
* add missing comment in WarpX.cpp
* add copyright and include guards to WarnManager_fwd.H
* cleaning and added comments to WarnManager.H
* updated test
* added fwd file for MsgLogger
* cleaning
* Added copyright in WarnManager.cpp
* Cleaning
* cleaning
* Add missing copyright
* cleaning
* clean and add comments to MsgLoggerSerialization.H
* cleaning MsgLogger_fwd.H
* cleaning
* continue cleaning MsgLogger
* added comments & bugfixing
* removed test file
* fixed bugs
* Update Source/Utils/MsgLogger/MsgLogger.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/WarpX.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLogger.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLogger.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLogger.cpp
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLoggerSerialization.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLoggerSerialization.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLoggerSerialization.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLoggerSerialization.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/MsgLogger/MsgLoggerSerialization.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/WarnManager.cpp
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/WarnManager.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/WarnManager.H
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Update Source/Utils/WarnManager.cpp
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* fixed bugs
* Update Source/Utils/MsgLogger/MsgLogger.cpp
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
* Added comment to explain rotation
* use last value computed by partial_sum
* fix bug
* now use error stream
* using anonymous namespace for helper functions
* print on both stderr and stdout
* now using runtime parameter to always print warnings
* added documentation
* using long int for counter
* sort affected warnings list
* add doc entry
* removed doc
* added documentation
* fixed bug
* fixed bug
* removing unnecessary text
* reformatting
* reformatting
* fixed bug
* fixed bug
* correction
* add warning_logger.rst to toctree in developers.rst
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
Diffstat (limited to 'Source/Utils/MsgLogger/MsgLoggerSerialization.H')
-rw-r--r-- | Source/Utils/MsgLogger/MsgLoggerSerialization.H | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/Source/Utils/MsgLogger/MsgLoggerSerialization.H b/Source/Utils/MsgLogger/MsgLoggerSerialization.H new file mode 100644 index 000000000..fba8bb0d1 --- /dev/null +++ b/Source/Utils/MsgLogger/MsgLoggerSerialization.H @@ -0,0 +1,189 @@ +/* Copyright 2021 Luca Fedeli + * + * This file is part of WarpX. + * + * License: BSD-3-Clause-LBNL + */ + +#ifndef WARPX_MSG_LOGGER_SERIALIZATION_H_ +#define WARPX_MSG_LOGGER_SERIALIZATION_H_ + +#include <algorithm> +#include <array> +#include <cstring> +#include <string> +#include <type_traits> +#include <vector> + +namespace Utils{ +namespace MsgLogger{ + + /** + * This function transforms a variable of type T into a vector of chars holding its + * byte representation and it appends this vector at the end of an + * existing vector of chars. T must be either a trivially copyable type or an std::string + * (see specialization) + * + * @tparam T the variable type + * @param[in] val a variable of type T to be serialized + * @param[in, out] vec a reference to the vector to which the byte representation of val is appended + */ + template <typename T> + void put_in(const T& val, std::vector<char>& vec) + { + static_assert(std::is_trivially_copyable<T>(), + "Cannot serialize non-trivally copyable types, except std::string."); + + const auto* ptr_val = reinterpret_cast<const char*>(&val); + vec.insert(vec.end(), ptr_val, ptr_val+sizeof(T)); + } + + /** + * This function transforms a string into a vector of chars holding its + * byte representation and it appends this vector at the end of an + * existing vector of chars (specialization of put_in<T>). + * + * @param[in] val a std::string to be serialized + * @param[in, out] vec a reference to the vector to which the byte representation of val is appended + */ + template <> + inline void put_in<std::string> (const std::string& val, std::vector<char>& vec) + { + const char* c_str = val.c_str(); + const auto length = static_cast<int>(val.size()); + + put_in(length, vec); + vec.insert(vec.end(), c_str, c_str+length); + } + + /** + * This function transforms an std::vector<T> into a vector of chars holding its + * byte representation and it appends this vector at the end of an + * existing vector of chars. T must be either a trivially copyable type or an std::string. + * A specialization exists in case val is a vector of chars. + * + * @tparam T the variable type + * @param[in] val a variable of type T to be serialized + * @param[in, out] vec a reference to the vector to which the byte representation of val is appended + */ + template <typename T> + inline void put_in_vec (const std::vector<T>& val, std::vector<char>& vec) + { + static_assert(std::is_trivially_copyable<T>() || std::is_same<T,std::string>(), + "Cannot serialize vectors of non-trivally copyable types" + ", except vectors of std::string."); + + put_in(static_cast<int>(val.size()), vec); + for (const auto& el : val) + put_in(el, vec); + } + + /** + * This function transforms an std::vector<char> into a vector of chars holding its + * byte representation and it appends this vector at the end of an + * existing vector of chars (specialization of put_in_vec<T>). + * + * @tparam T the variable type + * @param[in] val a variable of type T to be serialized + * @param[in, out] vec a reference to the vector to which the byte representation of val is appended + */ + template <> + inline void put_in_vec <char> (const std::vector<char>& val, std::vector<char>& vec) + { + put_in(static_cast<int>(val.size()), vec); + vec.insert(vec.end(), val.begin(), val.end()); + } + + /** + * This function extracts a variable of type T from a byte vector, at the position + * given by a std::vector<char> iterator. The iterator is then advanced according to + * the number of bytes read from the byte vector. T must be either a trivially copyable type + * or an std::string (see specialization below). + * + * @tparam T the variable type (must be trivially copyable) + * @param[in, out] it the iterator to a byte vector + * @return the variable extracted from the byte array + */ + template<typename T> + T get_out(std::vector<char>::const_iterator& it) + { + static_assert(std::is_trivially_copyable<T>(), + "Cannot extract non-trivally copyable types from char vectors," + " with the exception of std::string."); + + auto temp = std::array<char, sizeof(T)>{}; + std::copy(it, it + sizeof(T), temp.begin()); + it += sizeof(T); + T res; + std::memcpy(&res, temp.data(), sizeof(T)); + + return res; + } + + /** + * This function extracts an std::string from a byte vector, at the position + * given by a std::vector<char> iterator. The iterator is then advanced according to + * the number of bytes read from the byte vector. This is a specialization of + * get_out<T> + * + * @param[in, out] it the iterator to a byte vector + * @return the std::string extracted from the byte array + */ + template<> + inline std::string get_out<std::string> (std::vector<char>::const_iterator& it) + { + const auto length = get_out<int> (it); + const auto str = std::string{it, it+length}; + it += length; + + return str; + } + + /** + * This function extracts an std::vector<T> from a byte vector, at the position + * given by a std::vector<char> iterator. The iterator is then advanced according to + * the number of bytes read from the byte vector. T must be either a trivially copyable type + * or an std::string. + * + * @tparam T the variable type (must be trivially copyable) + * @param[in, out] it the iterator to a byte vector + * @return the variable extracted from the byte array + */ + template<typename T> + inline std::vector<T> get_out_vec (std::vector<char>::const_iterator& it) + { + static_assert(std::is_trivially_copyable<T>() || std::is_same<T,std::string>(), + "Cannot extract non-trivally copyable types from char vectors," + " with the exception of std::string."); + + const auto length = get_out<int> (it); + std::vector<T> res(length); + for (int i = 0; i < length; ++i) + res[i] = get_out<T>(it); + + return res; + } + + /** + * This function extracts an std::vector<char> from a byte vector, at the position + * given by a std::vector<char> iterator. The iterator is then advanced according to + * the number of bytes read from the byte vector. This is a specialization of get_out_vec<T>. + * + * @param[in, out] it the iterator to a byte vector + * @return the variable extracted from the byte array + */ + template<> + inline std::vector<char> get_out_vec<char> (std::vector<char>::const_iterator& it) + { + const auto length = get_out<int> (it); + std::vector<char> res(length); + std::copy(it, it+length, res.begin()); + it += length; + + return res; + } + +} +} + +#endif //WARPX_MSG_LOGGER_SERIALIZATION_H_ |