source: cpp/common/framsg.cpp @ 271

Last change on this file since 271 was 244, checked in by Maciej Komosinski, 11 years ago

Printing multi-line messages splits the message into separate lines and adds "..." prefix

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include "framsg.h"
6#include <common/nonstd_stdio.h>
7#include "stl-util.h"
8#include "Convert.h"
9
10const char* MSG_LEVEL[]={"DEBUG","INFO","WARN","ERROR","CRITICAL"};
11
12void FramMessage(const char *o, const char *m, const char *txt, int w)
13{
14        int line = 0; //all lines except the first one get the "..." prefix
15        const char* nextsep;
16        do
17        {
18                nextsep = strchr(txt, '\n');
19                if (nextsep == NULL) //last chunk, until the end
20                        nextsep = strchr(txt, '\0');
21                if ((nextsep > txt) && (nextsep[-1] == '\r'))
22                        nextsep--;
23                if (line == 0)
24                {
25                        if (*nextsep == 0) //there was only one line! no need to modify it in any way.
26                                _FramMessageSingleLine(o, m, txt, w);
27                        else //first line from multi-line
28                                _FramMessageSingleLine(o, m, string(txt, nextsep - txt).c_str(), w);
29                }
30                else //consecutive lines from multi-line
31                        _FramMessageSingleLine(o, m, ("..." + string(txt, nextsep - txt)).c_str(), w); //could also add line numbers like ...(3)... but let's keep the prefix short and simple
32                line++;
33                if ((nextsep[0] == '\r') && (nextsep[1] == '\n'))
34                        txt = nextsep + 2;
35                else if (*nextsep)
36                        txt = nextsep + 1;
37        } while (*nextsep);
38}
39
40
41void FMprintf_va(const char *o,const char *m,int w,const char *bl,va_list va)
42{
43        string buf=ssprintf_va(bl,va);
44        FramMessage(o,m,buf.c_str(),w);
45}
46
47void FMprintf(const char *o,const char *m,int w,const char *bl, ...)
48{
49        va_list argptr;
50        va_start(argptr,bl);
51        FMprintf_va(o,m,w,bl,argptr);
52        va_end(argptr);
53}
54
55void printFM(const char *bl,...)
56{
57        va_list argptr;
58        va_start(argptr,bl);
59        FMprintf_va("Message","printf",FMLV_INFO,bl,argptr);
60        va_end(argptr);
61}
Note: See TracBrowser for help on using the repository browser.