1//============================================================================
2//
3// SSSS tt lll lll
4// SS SS tt ll ll
5// SS tttttt eeee ll ll aaaa
6// SSSS tt ee ee ll ll aa
7// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
8// SS SS tt ee ll ll aa aa
9// SSSS ttt eeeee llll llll aaaaa
10//
11// Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony
12// and the Stella Team
13//
14// See the file "License.txt" for information on usage and redistribution of
15// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16//============================================================================
17
18#include "bspf.hxx"
19#include "Dialog.hxx"
20#include "FSNode.hxx"
21#include "GuiObject.hxx"
22#include "OSystem.hxx"
23#include "FrameBuffer.hxx"
24#include "Settings.hxx"
25#include "PopUpWidget.hxx"
26#include "StringListWidget.hxx"
27#include "StringParser.hxx"
28#include "Widget.hxx"
29#include "Font.hxx"
30#include "Logger.hxx"
31#include "LoggerDialog.hxx"
32
33// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
34LoggerDialog::LoggerDialog(OSystem& osystem, DialogContainer& parent,
35 const GUI::Font& font, int max_w, int max_h,
36 bool uselargefont)
37 : Dialog(osystem, parent, font, "System logs"),
38 myLogInfo(nullptr)
39{
40 const int lineHeight = font.getLineHeight(),
41 buttonWidth = font.getStringWidth("Save log to disk") + 20,
42 buttonHeight = font.getLineHeight() + 4;
43 int xpos, ypos;
44 WidgetArray wid;
45
46 // Set real dimensions
47 // This is one dialog that can take as much space as is available
48 setSize(4000, 4000, max_w, max_h);
49
50 // Test listing of the log output
51 xpos = 10; ypos = 10 + _th;
52 myLogInfo = new StringListWidget(this, uselargefont ? font :
53 instance().frameBuffer().infoFont(), xpos, ypos, _w - 2 * xpos,
54 _h - buttonHeight - ypos - 20 - 2 * lineHeight, false);
55 myLogInfo->setEditable(false);
56 wid.push_back(myLogInfo);
57 ypos += myLogInfo->getHeight() + 8;
58
59 // Level of logging (how much info to print)
60 VariantList items;
61 VarList::push_back(items, "None", int(Logger::Level::ERR));
62 VarList::push_back(items, "Basic", int(Logger::Level::INFO));
63 VarList::push_back(items, "Verbose", int(Logger::Level::DEBUG));
64 myLogLevel =
65 new PopUpWidget(this, font, xpos, ypos, font.getStringWidth("Verbose"),
66 lineHeight, items, "Log level ",
67 font.getStringWidth("Log level "));
68 wid.push_back(myLogLevel);
69
70 // Should log output also be shown on the console?
71 xpos += myLogLevel->getWidth() + 32;
72 myLogToConsole = new CheckboxWidget(this, font, xpos, ypos + 1, "Print to console");
73 wid.push_back(myLogToConsole);
74
75 // Add Save, OK and Cancel buttons
76 ButtonWidget* b;
77 b = new ButtonWidget(this, font, 10, _h - buttonHeight - 10,
78 buttonWidth, buttonHeight, "Save log to disk",
79 GuiObject::kDefaultsCmd);
80 wid.push_back(b);
81 addOKCancelBGroup(wid, font);
82
83 addToFocusList(wid);
84}
85
86// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
87void LoggerDialog::loadConfig()
88{
89 StringParser parser(instance().logMessages());
90 myLogInfo->setList(parser.stringList());
91 myLogInfo->setSelected(0);
92 myLogInfo->scrollToEnd();
93
94 myLogLevel->setSelected(instance().settings().getString("loglevel"), int(Logger::Level::INFO));
95 myLogToConsole->setState(instance().settings().getBool("logtoconsole"));
96}
97
98// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99void LoggerDialog::saveConfig()
100{
101 instance().settings().setValue("loglevel",
102 myLogLevel->getSelectedTag().toString());
103 instance().settings().setValue("logtoconsole", myLogToConsole->getState());
104}
105
106// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
107void LoggerDialog::saveLogFile()
108{
109 ostringstream path;
110 path << instance().defaultSaveDir() << "stella.log";
111 FilesystemNode node(path.str());
112
113 ofstream out(node.getPath());
114 if(out.is_open())
115 {
116 out << instance().logMessages();
117 instance().frameBuffer().showMessage("Saving log file to " + path.str());
118 }
119}
120
121// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
122void LoggerDialog::handleCommand(CommandSender* sender, int cmd,
123 int data, int id)
124{
125 switch(cmd)
126 {
127 case GuiObject::kOKCmd:
128 saveConfig();
129 close();
130 break;
131
132 case GuiObject::kDefaultsCmd:
133 saveLogFile();
134 break;
135
136 default:
137 Dialog::handleCommand(sender, cmd, data, 0);
138 break;
139 }
140}
141