source: trunk/Cbc/src/CbcGenMessages.cpp

Last change on this file was 2465, checked in by unxusr, 5 months ago

script to format sources

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/*! \legal
2  Copyright (C) 2007
3  Lou Hafer, International Business Machines Corporation and others. All
4  Rights Reserved.
5
6  This code is licensed under the terms of the Eclipse Public License (EPL).
7
8  $Id: CbcGenMessages.cpp 2465 2019-01-03 19:26:52Z forrest $
9*/
10/*
11  This file is part of cbc-generic.
12*/
13
14#include "CbcGenMessages.hpp"
15
16#include "CbcGenCtlBlk.hpp"
17
18namespace {
19
20char svnid[] = "$Id: CbcGenMessages.cpp 2465 2019-01-03 19:26:52Z forrest $";
21
22}
23
24/*
25  Begin file local namespace
26*/
27namespace {
28
29/*
30  Message definitions.
31
32  The precise form isn't important here, so long as the method that loads them
33  into a CoinMessages object can come up with values for external ID,
34  detail level, and format string. The use of an enum to provide an internal ID
35  for each message is mainly useful with the internationalisation feature. It
36  makes it easy to slap the same ID on alternate versions of a message.
37*/
38
39typedef struct {
40  CbcGenMsgCode inID;
41  int exID;
42  int lvl;
43  const char *fmt;
44} MsgDefn;
45
46static MsgDefn us_en_defns[] = {
47  // informational (0 -- 2999)
48  { CBCGEN_TEST_MSG, 1, 2, "This is the us_en test message, eh." },
49  { CBCGEN_NEW_SOLVER, 2, 2, "Solver is now \"%s\"." },
50  // warning (3000 -- 5999)
51  // Non-fatal errors (6000 -- 8999)
52  // Fatal errors (9000 and up)
53  { CBCGEN_CONFUSION, 9001, 1, "Internal confusion, line %d." },
54  { CBCGEN_DUMMY_END, 999999, 0, "" }
55};
56
57/*
58  We seem to need a dummy CoinMessages object to prevent the compiler from
59  complaining that CoinMessages::Language is unintialised.
60
61  const CoinMessages dummy(0) ;
62*/
63/*
64  The author is Canadian, eh. But we'll go with us_en anyways.
65*/
66const CoinMessages::Language default_language = CoinMessages::us_en;
67
68} /* End file local namespace */
69
70/*!
71  This function constructs a CoinMessages object filled with a default set of
72  messages, overlaid with whatever is available for the specified language.
73  It is used to establish the initial set of messages, and is also called
74  whenever the language is changed. The latter, because there's no way of
75  guaranteeing that the message sets for alternate languages will all
76  replace the same messages. This approach guarantees that the set of
77  messages is always composed of the default language overlaid with any
78  messages for an alternate language.
79
80  The default for lang is us_en, specified up in CbcGenCtlBlk.hpp. If you want
81  to change the default language, change the declaration there. That said,
82  you'll also have to provide the necessary message definitions and augment the
83  case statements below.
84*/
85
86void CbcGenCtlBlk::setMessages(CoinMessages::Language lang)
87
88{
89  /*
90      If messages exist, in the correct language, we have nothing more to do.
91    */
92  if (msgs_ && cur_lang_ == lang) {
93    return;
94  }
95  /*
96      Otherwise, we need to do a wholesale rebuild. Create a new object of the
97      appropriate size.
98    */
99  CoinMessages *msgs = new CoinMessages(sizeof(us_en_defns) / sizeof(MsgDefn));
100
101  msgs->setLanguage(lang);
102  strcpy(msgs->source_, "CbcG");
103  /*
104      Yes, this is gloriously redundant, but it's set up in anticipation of
105      future extensions.
106    */
107  MsgDefn *msgdefn;
108  switch (lang) {
109  case CoinMessages::us_en: {
110    msgdefn = us_en_defns;
111    break;
112  }
113  default: {
114    msgdefn = us_en_defns;
115    break;
116  }
117  }
118  /*
119      Open a loop to create and load the messages.
120    */
121  while (msgdefn->inID != CBCGEN_DUMMY_END) {
122    CoinOneMessage msg(msgdefn->exID, msgdefn->lvl, msgdefn->fmt);
123    msgs->addMessage(msgdefn->inID, msg);
124    msgdefn++;
125  }
126  /*
127      Now, if the local language differs from the default language, load any
128      overrides. Again, useless now, but maybe in the future ...
129    */
130  if (lang != cur_lang_) {
131    switch (lang) {
132    case CoinMessages::us_en: {
133      msgdefn = us_en_defns;
134      break;
135    }
136    default: {
137      msgdefn = us_en_defns;
138      break;
139    }
140    }
141
142    while (msgdefn->inID != CBCGEN_DUMMY_END) {
143      msgs->replaceMessage(msgdefn->inID, msgdefn->fmt);
144      msgdefn++;
145    }
146  }
147  /*
148      Each CoinOneMessage has a fixed-length array to hold the message; by default
149      this is 400 chars. Convert to `compressed' CoinOneMessage objects where the
150      array is only as large as necessary. Any attempt to replace a message, or the
151      message text, will automatically trigger a decompress operation before doing
152      the replacement, but the messages will *not* be automatically recompressed.
153    */
154  msgs->toCompact();
155  msgs_ = msgs;
156
157  return;
158}
159
160/*
161  Replaces the current message handler with the handler supplied as a
162  parameter. If ourMsgHandler_ is true, the existing handler is destroyed.
163*/
164
165void CbcGenCtlBlk::passInMessageHandler(CoinMessageHandler *newMsgHandler)
166
167{
168  if (msgHandler_ && ourMsgHandler_) {
169    delete msgHandler_;
170  }
171
172  msgHandler_ = newMsgHandler;
173  ourMsgHandler_ = false;
174
175  return;
176}
177
178/*
179  Start a message. This routine buries the whole business of locating the
180  message handler and messages, getting the log level right, etc. If, by some
181  chance, messages are not yet loaded, do so.
182*/
183
184CoinMessageHandler &CbcGenCtlBlk::message(CbcGenMsgCode inID)
185
186{
187  if (!msgs_) {
188    setMessages();
189  }
190  msgHandler_->setLogLevel(logLvl_);
191
192  msgHandler_->message(inID, *msgs_);
193
194  return (*msgHandler_);
195}
196
197/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
198*/
Note: See TracBrowser for help on using the repository browser.