LCOV - code coverage report
Current view: top level - as2js/lib - message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 101 101 100.0 %
Date: 2014-08-10 Functions: 30 34 88.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* message.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : #include "as2js/message.h"
      37             : 
      38             : 
      39             : namespace as2js
      40             : {
      41             : 
      42             : namespace
      43             : {
      44             :     MessageCallback *   g_message_callback = nullptr;
      45             :     message_level_t     g_maximum_message_level = message_level_t::MESSAGE_LEVEL_INFO;
      46             :     int                 g_warning_count = 0;
      47             :     int                 g_error_count = 0;
      48             : }
      49             : // no name namespace
      50             : 
      51             : 
      52             : /** \brief Create a message object with the specified information.
      53             :  *
      54             :  * This function generates a message object that can be used to generate
      55             :  * a message with the << operator and then gets sent to the client using
      56             :  * the message callback function on destruction.
      57             :  *
      58             :  * The level can be set to any one of the message levels available in
      59             :  * the message_level_t enumeration. The special MESSAGE_LEVEL_OFF value
      60             :  * can be used to avoid the message altogether (can be handy when you
      61             :  * support a varying message level.)
      62             :  *
      63             :  * \param[in] message_level  The level of the message.
      64             :  * \param[in] error_code  An error code to print in the output message.
      65             :  * \param[in] pos  The position to which the message applies.
      66             :  */
      67    22872024 : Message::Message(message_level_t message_level, err_code_t error_code, Position const& pos)
      68             :     //: stringstream() -- auto-init
      69             :     : f_message_level(static_cast<int32_t>(message_level))
      70             :     , f_error_code(static_cast<int32_t>(error_code))
      71    11436012 :     , f_position(pos)
      72             : {
      73    11436012 : }
      74             : 
      75             : 
      76             : /** \brief Create a message object with the specified information.
      77             :  *
      78             :  * This function is an overload of the default constructor that does not
      79             :  * include the position information. This is used whenever we generate
      80             :  * an error from outside of the node tree, parser, etc.
      81             :  *
      82             :  * \param[in] message_level  The level of the message.
      83             :  * \param[in] error_code  An error code to print in the output message.
      84             :  */
      85       16472 : Message::Message(message_level_t message_level, err_code_t error_code)
      86             :     //: stringstream() -- auto-init
      87             :     : f_message_level(static_cast<int32_t>(message_level))
      88        8236 :     , f_error_code(static_cast<int32_t>(error_code))
      89             :     //, f_position()
      90             : {
      91        8236 : }
      92             : 
      93             : 
      94             : /** \brief Output the message created with the << operators.
      95             :  *
      96             :  * The destructor of the message object is where things happen. This function
      97             :  * prints out the message that was built using the different << operators
      98             :  * and the parameters specified in the constructor.
      99             :  *
     100             :  * The result is then passed to the message callback. If you did not setup
     101             :  * that function, the message is lost.
     102             :  *
     103             :  * If the level of the message was set to MESSAGE_LEVEL_OFF (usualy via
     104             :  * a command line option) then the message callback does not get called.
     105             :  */
     106    45776992 : Message::~Message()
     107             : {
     108             :     // actually emit the message
     109    11444248 :     if(g_message_callback                           // there is a callback?
     110    11433999 :     && message_level_t::MESSAGE_LEVEL_OFF != f_message_level         // level is off?!
     111    10385007 :     && f_message_level <= g_maximum_message_level   // level is large enough?
     112    22878247 :     && rdbuf()->in_avail() != 0)                    // there is a message?
     113             :     {
     114     5889326 :         if(f_position.get_filename().empty())
     115             :         {
     116     4088957 :             f_position.set_filename("unknown-file");
     117             :         }
     118     5889326 :         if(f_position.get_function().empty())
     119             :         {
     120     5885224 :             f_position.set_function("unknown-func");
     121             :         }
     122             : 
     123     5889326 :         switch(f_message_level)
     124             :         {
     125             :         case message_level_t::MESSAGE_LEVEL_FATAL:
     126             :         case message_level_t::MESSAGE_LEVEL_ERROR:
     127     5107274 :             ++g_error_count;
     128     5107274 :             break;
     129             : 
     130             :         case message_level_t::MESSAGE_LEVEL_WARNING:
     131      332484 :             ++g_warning_count;
     132      332484 :             break;
     133             : 
     134             :         // others are not currently counted
     135             :         default:
     136      449568 :             break;
     137             : 
     138             :         }
     139             : 
     140     5889326 :         g_message_callback->output(f_message_level, f_error_code, f_position, str());
     141             :     }
     142    22888496 : }
     143             : 
     144             : 
     145             : /** \brief Append an char string.
     146             :  *
     147             :  * This function appends an char string to the message.
     148             :  *
     149             :  * \param[in] v  A character string.
     150             :  *
     151             :  * \return A reference to the message.
     152             :  */
     153    10401864 : Message& Message::operator << (char const *s)
     154             : {
     155             :     // we assume UTF-8 because in our Snap environment most everything is
     156    10401864 :     static_cast<std::stringstream&>(*this) << s;
     157    10401864 :     return *this;
     158             : }
     159             : 
     160             : 
     161             : /** \brief Append an wchar_t string.
     162             :  *
     163             :  * This function appends an wchar_t string to the message.
     164             :  *
     165             :  * \param[in] v  A wchar_t string.
     166             :  *
     167             :  * \return A reference to the message.
     168             :  */
     169           2 : Message& Message::operator << (wchar_t const *s)
     170             : {
     171           2 :     String string;
     172           2 :     string.from_wchar(s);
     173           2 :     static_cast<std::stringstream&>(*this) << string.to_utf8();
     174           2 :     return *this;
     175             : }
     176             : 
     177             : 
     178             : /** \brief Append an std::string value.
     179             :  *
     180             :  * This function appends an std::string value to the message.
     181             :  *
     182             :  * \param[in] v  An std::string value.
     183             :  *
     184             :  * \return A reference to the message.
     185             :  */
     186           1 : Message& Message::operator << (std::string const& s)
     187             : {
     188           1 :     static_cast<std::stringstream&>(*this) << s;
     189           1 :     return *this;
     190             : }
     191             : 
     192             : 
     193             : /** \brief Append an std::wstring value.
     194             :  *
     195             :  * This function appends an std::wstring value to the message.
     196             :  *
     197             :  * \param[in] v  An std::wstring value.
     198             :  *
     199             :  * \return A reference to the message.
     200             :  */
     201           2 : Message& Message::operator << (std::wstring const& s)
     202             : {
     203           2 :     String string;
     204           2 :     string.from_wchar(s.c_str(), s.length());
     205           2 :     static_cast<std::stringstream&>(*this) << string.to_utf8();
     206           2 :     return *this;
     207             : }
     208             : 
     209             : 
     210             : /** \brief Append a String value.
     211             :  *
     212             :  * This function appends a String value to the message.
     213             :  *
     214             :  * \param[in] v  A String value.
     215             :  *
     216             :  * \return A reference to the message.
     217             :  */
     218      102670 : Message& Message::operator << (String const& s)
     219             : {
     220      102670 :     static_cast<std::stringstream&>(*this) << s.to_utf8();
     221      102670 :     return *this;
     222             : }
     223             : 
     224             : 
     225             : /** \brief Append a char value.
     226             :  *
     227             :  * This function appends a character to the message.
     228             :  *
     229             :  * \param[in] v  A char value.
     230             :  *
     231             :  * \return A reference to the message.
     232             :  */
     233        2386 : Message& Message::operator << (char const v)
     234             : {
     235        2386 :     static_cast<std::stringstream&>(*this) << v;
     236        2386 :     return *this;
     237             : }
     238             : 
     239             : 
     240             : /** \brief Append a signed char value.
     241             :  *
     242             :  * This function appends a signed char value to the message.
     243             :  *
     244             :  * \param[in] v  A signed char value.
     245             :  *
     246             :  * \return A reference to the message.
     247             :  */
     248         256 : Message& Message::operator << (signed char const v)
     249             : {
     250         256 :     static_cast<std::stringstream&>(*this) << static_cast<int>(v);
     251         256 :     return *this;
     252             : }
     253             : 
     254             : 
     255             : /** \brief Append a unsigned char value.
     256             :  *
     257             :  * This function appends a unsigned char value to the message.
     258             :  *
     259             :  * \param[in] v  A unsigned char value.
     260             :  *
     261             :  * \return A reference to the message.
     262             :  */
     263         256 : Message& Message::operator << (unsigned char const v)
     264             : {
     265         256 :     static_cast<std::stringstream&>(*this) << static_cast<int>(v);
     266         256 :     return *this;
     267             : }
     268             : 
     269             : 
     270             : /** \brief Append a signed short value.
     271             :  *
     272             :  * This function appends a signed short value to the message.
     273             :  *
     274             :  * \param[in] v  A signed short value.
     275             :  *
     276             :  * \return A reference to the message.
     277             :  */
     278         256 : Message& Message::operator << (signed short const v)
     279             : {
     280         256 :     static_cast<std::stringstream&>(*this) << static_cast<int>(v);
     281         256 :     return *this;
     282             : }
     283             : 
     284             : 
     285             : /** \brief Append a unsigned short value.
     286             :  *
     287             :  * This function appends a unsigned short value to the message.
     288             :  *
     289             :  * \param[in] v  A unsigned short value.
     290             :  *
     291             :  * \return A reference to the message.
     292             :  */
     293         256 : Message& Message::operator << (unsigned short const v)
     294             : {
     295         256 :     static_cast<std::stringstream&>(*this) << static_cast<int>(v);
     296         256 :     return *this;
     297             : }
     298             : 
     299             : 
     300             : /** \brief Append a signed int value.
     301             :  *
     302             :  * This function appends a signed int value to the message.
     303             :  *
     304             :  * \param[in] v  A signed int value.
     305             :  *
     306             :  * \return A reference to the message.
     307             :  */
     308     1114274 : Message& Message::operator << (signed int const v)
     309             : {
     310     1114274 :     static_cast<std::stringstream&>(*this) << v;
     311     1114274 :     return *this;
     312             : }
     313             : 
     314             : 
     315             : /** \brief Append a unsigned int value.
     316             :  *
     317             :  * This function appends a unsigned int value to the message.
     318             :  *
     319             :  * \param[in] v  A unsigned int value.
     320             :  *
     321             :  * \return A reference to the message.
     322             :  */
     323         256 : Message& Message::operator << (unsigned int const v)
     324             : {
     325         256 :     static_cast<std::stringstream&>(*this) << v;
     326         256 :     return *this;
     327             : }
     328             : 
     329             : 
     330             : /** \brief Append a signed long value.
     331             :  *
     332             :  * This function appends a signed long value to the message.
     333             :  *
     334             :  * \param[in] v  A signed long value.
     335             :  *
     336             :  * \return A reference to the message.
     337             :  */
     338         256 : Message& Message::operator << (signed long const v)
     339             : {
     340         256 :     static_cast<std::stringstream&>(*this) << v;
     341         256 :     return *this;
     342             : }
     343             : 
     344             : 
     345             : /** \brief Append a unsigned long value.
     346             :  *
     347             :  * This function appends a unsigned long value to the message.
     348             :  *
     349             :  * \param[in] v  A unsigned long value.
     350             :  *
     351             :  * \return A reference to the message.
     352             :  */
     353         256 : Message& Message::operator << (unsigned long const v)
     354             : {
     355         256 :     static_cast<std::stringstream&>(*this) << v;
     356         256 :     return *this;
     357             : }
     358             : 
     359             : 
     360             : /** \brief Append a signed long long value.
     361             :  *
     362             :  * This function appends a signed long long value to the message.
     363             :  *
     364             :  * \param[in] v  A signed long long value.
     365             :  *
     366             :  * \return A reference to the message.
     367             :  */
     368         256 : Message& Message::operator << (signed long long const v)
     369             : {
     370         256 :     static_cast<std::stringstream&>(*this) << v;
     371         256 :     return *this;
     372             : }
     373             : 
     374             : 
     375             : /** \brief Append an Int64 value.
     376             :  *
     377             :  * This function appends the value saved in an Int64 value.
     378             :  *
     379             :  * \parm[in] v  An as2js::Int64 value.
     380             :  */
     381         256 : Message& Message::operator << (Int64 const v)
     382             : {
     383         256 :     static_cast<std::stringstream&>(*this) << v.get();
     384         256 :     return *this;
     385             : }
     386             : 
     387             : 
     388             : /** \brief Append a unsigned long long value.
     389             :  *
     390             :  * This function appends a unsigned long long value to the message.
     391             :  *
     392             :  * \param[in] v  A unsigned long long value.
     393             :  *
     394             :  * \return A reference to the message.
     395             :  */
     396         256 : Message& Message::operator << (unsigned long long const v)
     397             : {
     398         256 :     static_cast<std::stringstream&>(*this) << v;
     399         256 :     return *this;
     400             : }
     401             : 
     402             : 
     403             : /** \brief Append a float value.
     404             :  *
     405             :  * This function appends a float value to the message.
     406             :  *
     407             :  * \param[in] v  A float value.
     408             :  *
     409             :  * \return A reference to the message.
     410             :  */
     411         256 : Message& Message::operator << (float const v)
     412             : {
     413         256 :     static_cast<std::stringstream&>(*this) << v;
     414         256 :     return *this;
     415             : }
     416             : 
     417             : 
     418             : /** \brief Append a double value.
     419             :  *
     420             :  * This function appends a double value to the message.
     421             :  *
     422             :  * \param[in] v  A double value.
     423             :  *
     424             :  * \return A reference to the message.
     425             :  */
     426         256 : Message& Message::operator << (double const v)
     427             : {
     428         256 :     static_cast<std::stringstream&>(*this) << v;
     429         256 :     return *this;
     430             : }
     431             : 
     432             : 
     433             : /** \brief Append a Float64 value.
     434             :  *
     435             :  * This function appends the value saved in an Float64 value.
     436             :  *
     437             :  * \parm[in] v  An as2js::Float64 value.
     438             :  */
     439         256 : Message& Message::operator << (Float64 const v)
     440             : {
     441         256 :     static_cast<std::stringstream&>(*this) << v.get();
     442         256 :     return *this;
     443             : }
     444             : 
     445             : 
     446             : /** \brief Append a Boolean value.
     447             :  *
     448             :  * This function appends a Boolean value to the message as a 0 or a 1.
     449             :  *
     450             :  * \param[in] v  A Boolean value.
     451             :  *
     452             :  * \return A reference to the message.
     453             :  */
     454         256 : Message& Message::operator << (bool const v)
     455             : {
     456         256 :     static_cast<std::stringstream&>(*this) << static_cast<int>(v);;
     457         256 :     return *this;
     458             : }
     459             : 
     460             : 
     461             : /** \brief Setup the callback so tools can receive error messages.
     462             :  *
     463             :  * This function is used by external processes to setup a callback. The
     464             :  * callback receives the message output as generated by the Message
     465             :  * class.
     466             :  *
     467             :  * \sa configure()
     468             :  */
     469     5678903 : void Message::set_message_callback(MessageCallback *callback)
     470             : {
     471     5678903 :     g_message_callback = callback;
     472     5678903 : }
     473             : 
     474             : 
     475             : /** \brief Define the maximum level a message can be to be displayed.
     476             :  *
     477             :  * This function is used to change the maximum level a message can
     478             :  * be in order to be displayed. Messages with a larger level are
     479             :  * completely ignored.
     480             :  *
     481             :  * Note that errors and fatal errors cannot be ignored using this
     482             :  * mechanism (i.e. the smallest possible value for max_level is
     483             :  * MESSAGE_LEVEL_ERROR.)
     484             :  *
     485             :  * \param[in] max_level  The maximum level a message can have.
     486             :  */
     487     3671473 : void Message::set_message_level(message_level_t max_level)
     488             : {
     489             :     g_maximum_message_level = max_level < message_level_t::MESSAGE_LEVEL_ERROR
     490             :                             ? message_level_t::MESSAGE_LEVEL_ERROR
     491     3671473 :                             : max_level;
     492     3671473 : }
     493             : 
     494             : 
     495             : /** \brief The number of warnings that were found so far.
     496             :  *
     497             :  * This function returns the number of warnings that were
     498             :  * processed so far.
     499             :  *
     500             :  * \return The number of warnings that were processed so far.
     501             :  */
     502     3171934 : int Message::warning_count()
     503             : {
     504     3171934 :     return g_warning_count;
     505             : }
     506             : 
     507             : 
     508             : /** \brief The number of errors that were found so far.
     509             :  *
     510             :  * This function returns the number of errors and fatal errors that were
     511             :  * processed so far.
     512             :  *
     513             :  * \return The number of errors that were processed so far.
     514             :  */
     515     7946724 : int Message::error_count()
     516             : {
     517     7946724 :     return g_error_count;
     518             : }
     519             : 
     520             : 
     521             : 
     522             : 
     523          20 : }
     524             : // namespace as2js
     525             : 
     526             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.9