| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <iosfwd> |
| 14 |
#include <string> |
| 15 |
#include "eckit/log/CodeLocation.h" |
| 16 |
|
| 17 |
namespace atlas { |
| 18 |
|
| 19 |
class CodeLocation { |
| 20 |
public: |
| 21 |
CodeLocation(const CodeLocation& loc): CodeLocation(loc.file(), loc.line(), loc.func(), loc.stored_) {} |
| 22 |
CodeLocation(const eckit::CodeLocation& loc): loc_(loc), stored_(false) {} |
| 23 |
CodeLocation(const char* file, int line, const char* function, bool store = false): stored_(store) { |
| 24 |
if (stored_) { |
| 25 |
if (file) { |
| 26 |
file_str_ = std::string(file); |
| 27 |
file_ = file_str_.c_str(); |
| 28 |
} |
| 29 |
if (function) { |
| 30 |
function_str_ = std::string(function); |
| 31 |
function_ = function_str_.c_str(); |
| 32 |
} |
| 33 |
loc_ = eckit::CodeLocation(file_, line, function_); |
| 34 |
} |
| 35 |
else { |
| 36 |
loc_ = eckit::CodeLocation(file, line, function); |
| 37 |
} |
| 38 |
} |
| 39 |
operator const eckit::CodeLocation&() const { return loc_; } |
| 40 |
|
| 41 |
/// conversion to bool for checking if location was set |
| 42 |
operator bool() const { return loc_; } |
| 43 |
|
| 44 |
std::string asString() const { return loc_.asString(); } |
| 45 |
/// accessor to line |
| 46 |
int line() const { return loc_.line(); } |
| 47 |
/// accessor to file |
| 48 |
const char* file() const { return loc_.file(); } |
| 49 |
/// accessor to function |
| 50 |
const char* func() const { return loc_.func(); } |
| 51 |
friend std::ostream& operator<<(std::ostream& s, const CodeLocation& loc); |
| 52 |
|
| 53 |
private: |
| 54 |
eckit::CodeLocation loc_; |
| 55 |
bool stored_ = false; |
| 56 |
const char* file_ = nullptr; |
| 57 |
const char* function_ = nullptr; |
| 58 |
std::string file_str_; |
| 59 |
std::string function_str_; |
| 60 |
}; |
| 61 |
|
| 62 |
} // namespace atlas |