source: trunk/Cbc/src/CbcGenMessages.cpp @ 1899

Last change on this file since 1899 was 1899, checked in by stefan, 6 years ago

fixup svn properties

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 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 1899 2013-04-09 18:12:08Z stefan $
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 1899 2013-04-09 18:12:08Z stefan $" ;
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
39
40typedef struct {
41    CbcGenMsgCode inID ;
42    int exID ;
43    int lvl ;
44    const char *fmt ;
45} MsgDefn ;
46
47static MsgDefn us_en_defns[] = {
48    // informational (0 -- 2999)
49    { CBCGEN_TEST_MSG, 1, 2, "This is the us_en test message, eh." },
50    { CBCGEN_NEW_SOLVER, 2, 2, "Solver is now \"%s\"." },
51    // warning (3000 -- 5999)
52    // Non-fatal errors (6000 -- 8999)
53    // Fatal errors (9000 and up)
54    { CBCGEN_CONFUSION, 9001, 1, "Internal confusion, line %d." },
55    { CBCGEN_DUMMY_END, 999999, 0, "" }
56} ;
57
58/*
59  We seem to need a dummy CoinMessages object to prevent the compiler from
60  complaining that CoinMessages::Language is unintialised.
61
62  const CoinMessages dummy(0) ;
63*/
64/*
65  The author is Canadian, eh. But we'll go with us_en anyways.
66*/
67const CoinMessages::Language default_language = CoinMessages::us_en ;
68
69} /* End file local namespace */
70
71/*!
72  This function constructs a CoinMessages object filled with a default set of
73  messages, overlaid with whatever is available for the specified language.
74  It is used to establish the initial set of messages, and is also called
75  whenever the language is changed. The latter, because there's no way of
76  guaranteeing that the message sets for alternate languages will all
77  replace the same messages. This approach guarantees that the set of
78  messages is always composed of the default language overlaid with any
79  messages for an alternate language.
80
81  The default for lang is us_en, specified up in CbcGenCtlBlk.hpp. If you want
82  to change the default language, change the declaration there. That said,
83  you'll also have to provide the necessary message definitions and augment the
84  case statements below.
85*/
86
87void CbcGenCtlBlk::setMessages (CoinMessages::Language lang)
88
89{
90    /*
91      If messages exist, in the correct language, we have nothing more to do.
92    */
93    if (msgs_ && cur_lang_ == lang) {
94        return ;
95    }
96    /*
97      Otherwise, we need to do a wholesale rebuild. Create a new object of the
98      appropriate size.
99    */
100    CoinMessages *msgs = new CoinMessages(sizeof(us_en_defns) / sizeof(MsgDefn)) ;
101
102    msgs->setLanguage(lang) ;
103    strcpy(msgs->source_, "CbcG");
104    /*
105      Yes, this is gloriously redundant, but it's set up in anticipation of
106      future extensions.
107    */
108    MsgDefn *msgdefn ;
109    switch (lang) {
110    case CoinMessages::us_en: {
111        msgdefn = us_en_defns ;
112        break ;
113    }
114    default: {
115        msgdefn = us_en_defns ;
116        break ;
117    }
118    }
119    /*
120      Open a loop to create and load the messages.
121    */
122    while (msgdefn->inID != CBCGEN_DUMMY_END) {
123        CoinOneMessage msg(msgdefn->exID, msgdefn->lvl, msgdefn->fmt) ;
124        msgs->addMessage(msgdefn->inID, msg) ;
125        msgdefn++ ;
126    }
127    /*
128      Now, if the local language differs from the default language, load any
129      overrides. Again, useless now, but maybe in the future ...
130    */
131    if (lang != cur_lang_) {
132        switch (lang) {
133        case CoinMessages::us_en: {
134            msgdefn = us_en_defns ;
135            break;
136        }
137        default: {
138            msgdefn = us_en_defns ;
139            break;
140        }
141        }
142
143        while (msgdefn->inID != CBCGEN_DUMMY_END) {
144            msgs->replaceMessage(msgdefn->inID, msgdefn->fmt) ;
145            msgdefn++ ;
146        }
147    }
148    /*
149      Each CoinOneMessage has a fixed-length array to hold the message; by default
150      this is 400 chars. Convert to `compressed' CoinOneMessage objects where the
151      array is only as large as necessary. Any attempt to replace a message, or the
152      message text, will automatically trigger a decompress operation before doing
153      the replacement, but the messages will *not* be automatically recompressed.
154    */
155    msgs->toCompact() ;
156    msgs_ = msgs ;
157
158    return ;
159}
160
161
162/*
163  Replaces the current message handler with the handler supplied as a
164  parameter. If ourMsgHandler_ is true, the existing handler is destroyed.
165*/
166
167void CbcGenCtlBlk::passInMessageHandler (CoinMessageHandler *newMsgHandler)
168
169{
170    if (msgHandler_ && ourMsgHandler_) {
171        delete msgHandler_ ;
172    }
173
174    msgHandler_ = newMsgHandler ;
175    ourMsgHandler_ = false ;
176
177    return ;
178}
179
180/*
181  Start a message. This routine buries the whole business of locating the
182  message handler and messages, getting the log level right, etc. If, by some
183  chance, messages are not yet loaded, do so.
184*/
185
186CoinMessageHandler &CbcGenCtlBlk::message (CbcGenMsgCode inID)
187
188{
189    if (!msgs_) {
190        setMessages() ;
191    }
192    msgHandler_->setLogLevel(logLvl_) ;
193
194    msgHandler_->message(inID, *msgs_) ;
195
196    return (*msgHandler_) ;
197}
198
Note: See TracBrowser for help on using the repository browser.