TextException.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "base/TextException.h"
11 #include "Debug.h"
12 #include "sbuf/SBuf.h"
13 #include "util.h"
14 
16 {
17  message=NULL;
19  theLineNo=0;
20  theId=0;
21 }
22 
24  message((right.message?xstrdup(right.message):NULL)), theFileName(right.theFileName), theLineNo(right.theLineNo), theId(right.theId)
25 {
26 }
27 
28 TextException::TextException(const char *aMsg, const char *aFileName, int aLineNo, unsigned int anId):
29  message(aMsg?xstrdup(aMsg):NULL), theFileName(aFileName), theLineNo(aLineNo), theId(anId)
30 {}
31 
32 TextException::TextException(SBuf msg, const char *aFileName, int aLineNo, unsigned int anId):
33  TextException(msg.c_str(), aFileName, aLineNo, anId)
34 {}
35 
37 {
38  if (message) xfree(message);
39 }
40 
42 {
43  if (this==&right) return *this;
44  if (message) xfree(message);
45  message=(right.message?xstrdup(right.message):NULL);
47  theLineNo=right.theLineNo;
48  theId=right.theId;
49  return *this;
50 }
51 
52 const char *TextException::what() const throw()
53 {
55  return message ? message : "TextException without a message";
56 }
57 
58 unsigned int TextException::FileNameHash(const char *fname)
59 {
60  const char *s = NULL;
61  unsigned int n = 0;
62  unsigned int j = 0;
63  unsigned int i = 0;
64  s = strrchr(fname, '/');
65 
66  if (s)
67  ++s;
68  else
69  s = fname;
70 
71  while (*s) {
72  ++j;
73  n ^= 271 * (unsigned) *s;
74  ++s;
75  }
76  i = n ^ (j * 271);
77  /*18bits of a 32 bit integer used for filename hash (max hash=262143),
78  and 14 bits for storing line number (16k max).
79  If you change this policy remember to update the FileNameHash function
80  in the scripts/calc-must-ids.pl script
81  */
82  return i % 262143;
83 }
84 
85 void Throw(const char *message, const char *fileName, int lineNo, unsigned int id)
86 {
87 
88  // or should we let the exception recepient print the exception instead?
89 
90  if (fileName) {
91  debugs(0, 3, fileName << ':' << lineNo << ": exception" <<
92  (message ? ": " : ".") << (message ? message : ""));
93  } else {
94  debugs(0, 3, "exception" <<
95  (message ? ": " : ".") << (message ? message : ""));
96  }
97 
98  throw TextException(message, fileName, lineNo, id);
99 }
100 
TextException & operator=(const TextException &right)
const char * theFileName
Definition: TextException.h:48
Definition: SBuf.h:87
int i
Definition: membanger.c:49
#define xstrdup
virtual ~TextException()
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
static unsigned int FileNameHash(const char *fname)
a small integer hash value to semi-uniquely identify the source file
unsigned int theId
Definition: TextException.h:50
void Throw(const char *message, const char *fileName, int lineNo, unsigned int id)
virtual const char * what() const
#define xfree
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors