summaryrefslogtreecommitdiff
path: root/lib/d/src/thrift/base.d
blob: b39b1f13d5490695eab909ced683264d2992bf50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */
module thrift.base;

/**
 * Common base class for all Thrift exceptions.
 */
class TException : Exception {
  ///
  this(string msg = "", string file = __FILE__, size_t line = __LINE__,
    Throwable next = null)
  {
    super(msg, file, line, next);
  }
}

/**
 * An operation failed because one or more sub-tasks failed.
 */
class TCompoundOperationException : TException {
  ///
  this(string msg, Exception[] exceptions, string file = __FILE__,
    size_t line = __LINE__, Throwable next = null)
  {
    super(msg, file, line, next);
    this.exceptions = exceptions;
  }

  /// The exceptions thrown by the children of the operation. If applicable,
  /// the list is ordered in the same way the exceptions occured.
  Exception[] exceptions;
}

/// The Thrift version string, used for informative purposes.
// Note: This is currently hardcoded, but will likely be filled in by the build
// system in future versions.
enum VERSION = "0.9.2";

/**
 * Functions used for logging inside Thrift.
 *
 * By default, the formatted messages are written to stdout/stderr, but this
 * behavior can be overwritten by providing custom g_{Info, Error}LogSink
 * handlers.
 *
 * Examples:
 * ---
 * logInfo("An informative message.");
 * logError("Some error occured: %s", e);
 * ---
 */
alias logFormatted!g_infoLogSink logInfo;
alias logFormatted!g_errorLogSink logError; /// Ditto

/**
 * Error and info log message sinks.
 *
 * These delegates are called with the log message passed as const(char)[]
 * argument, and can be overwritten to hook the Thrift libraries up with a
 * custom logging system. By default, they forward all output to stdout/stderr.
 */
__gshared void delegate(const(char)[]) g_infoLogSink;
__gshared void delegate(const(char)[]) g_errorLogSink; /// Ditto

shared static this() {
  import std.stdio;

  g_infoLogSink = (const(char)[] text) {
    stdout.writeln(text);
  };

  g_errorLogSink = (const(char)[] text) {
    stderr.writeln(text);
  };
}

// This should be private, if it could still be used through the aliases then.
template logFormatted(alias target) {
  void logFormatted(string file = __FILE__, int line = __LINE__,
    T...)(string format, T args) if (
    __traits(compiles, { target(""); })
  ) {
    import std.format, std.stdio;
    if (target !is null) {
      scope(exit) g_formatBuffer.clear();

      // Phobos @@BUG@@: If the empty string put() is removed, Appender.data
      // stays empty.
      g_formatBuffer.put("");

      formattedWrite(g_formatBuffer, "%s:%s: ", file, line);

      static if (T.length == 0) {
        g_formatBuffer.put(format);
      } else {
        formattedWrite(g_formatBuffer, format, args);
      }
      target(g_formatBuffer.data);
    }
  }
}

private {
  // Use a global, but thread-local buffer for constructing log messages.
  import std.array : Appender;
  Appender!(char[]) g_formatBuffer;
}