1 /** @file
2 *
3 * A brief file description
4 *
5 * @section license License
6 *
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements. See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership. The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License. You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24 #pragma once
25
26 #include "tscore/BufferWriterForward.h"
27
28 // The SourceLocation class wraps up a source code location, including
29 // file name, function name, and line number, and contains a method to
30 // format the result into a string buffer.
31
32 #define MakeSourceLocation() SourceLocation(__FILE__, __FUNCTION__, __LINE__)
33
34 class SourceLocation
35 {
36 public:
37 const char *file = nullptr;
38 const char *func = nullptr;
39 int line = 0;
40
41 SourceLocation() = default;
42 SourceLocation(const SourceLocation &rhs) = default;
43
SourceLocation(const char * _file,const char * _func,int _line)44 SourceLocation(const char *_file, const char *_func, int _line) : file(_file), func(_func), line(_line) {}
45
46 bool
valid()47 valid() const
48 {
49 return file && line;
50 }
51
52 SourceLocation &
53 operator=(const SourceLocation &rhs)
54 {
55 this->file = rhs.file;
56 this->func = rhs.func;
57 this->line = rhs.line;
58 return *this;
59 }
60
61 char *str(char *buf, int buflen) const;
62 ts::BufferWriter &print(ts::BufferWriter &w, ts::BWFSpec const &spec) const;
63 };
64
65 namespace ts
66 {
67 inline BufferWriter &
bwformat(BufferWriter & w,BWFSpec const & spec,SourceLocation const & loc)68 bwformat(BufferWriter &w, BWFSpec const &spec, SourceLocation const &loc)
69 {
70 return loc.print(w, spec);
71 }
72 } // namespace ts
73