staticlib_config  1.1.1
 All Classes Namespaces Files Functions Macros
tracemsg.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015, alex at staticlibs.net
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * File: tracemsg.hpp
19  * Author: alex
20  *
21  * Created on January 2, 2015, 11:53 AM
22  */
23 
24 #ifndef STATICLIB_CONFIG_TRACEMSG_HPP
25 #define STATICLIB_CONFIG_TRACEMSG_HPP
26 
27 #include <limits>
28 #include <string>
29 
31 #include "staticlib/config/os.hpp"
32 
33 #ifdef STATICLIB_ANDROID
34 #include <sstream>
35 #endif // STATICLIB_ANDROID
36 
37 #define TRACEMSG(message) staticlib::config::tracemsg(std::string() + message, __FILE__, STATICLIB_CURRENT_FUNCTION, __LINE__)
38 
39 namespace staticlib {
40 namespace config {
41 
52 inline std::string tracemsg(const std::string& message, const std::string& file, const std::string& func, int line) {
53  // cleanup function name
54  auto paren_ind = func.find("(");
55  auto fun_end_pos = std::string::npos != paren_ind ? paren_ind : message.length();
56  auto space_ind = func.find_last_of(" ", fun_end_pos);
57  auto fun_start_pos = (std::string::npos != space_ind && fun_end_pos > space_ind) ? space_ind + 1 : 0;
58  // cleanup filename
59  std::string::size_type filename_start_pos = 0;
60  auto slash_ind = file.find_last_of("/");
61  if (std::string::npos != slash_ind && file.length() > slash_ind) {
62  filename_start_pos = slash_ind + 1;
63  } else {
64  auto backslash_ind = file.find_last_of("\\");
65  if (std::string::npos != backslash_ind && file.length() > backslash_ind) {
66  filename_start_pos = backslash_ind + 1;
67  }
68  }
69  // to_string moved to sl::support
70 #ifdef STATICLIB_ANDROID
71  std::stringstream linestream;
72  linestream << line;
73  std::string linest = linestream.str();
74 #else
75  std::string linest = std::to_string(line);
76 #endif // STATICLIB_ANDROID
77  // format message
78  return std::string()
79  .append(message)
80  .append("\n at ")
81  .append(func, fun_start_pos, fun_end_pos - fun_start_pos)
82  .append("(")
83  .append(file, filename_start_pos, file.length() - filename_start_pos)
84  .append(":")
85  .append(linest)
86  .append(")");
87 }
88 
89 } //namespace
90 }
91 
92 #endif /* STATICLIB_CONFIG_TRACEMSG_HPP */