Qt-interest Archive, March 2007
Encoding trouble qDebug, console, WindowsXP/2000, cp1251, cp866, KOI8-R
Pages: Prev | 1 | 2 | Next
Message 1 in thread
I redirect debug out with qInstallMsgHandler() to log.txt file
My sources saved in Windows-cp1251 encode and have russian words,
i write program in Windows.
When i open log.txt file i see not readable characters.
I'm try use this code for converting to DOS encode:
QTextCodec::setCodecForCStrings( QTextCodec::codecForName("IBM 866") );
but this also not readable.
Well i save my sources in KOI8-R encode and write this code:
QTextCodec::setCodecForCStrings( QTextCodec::codecForName("KOI8-R") );
And i see russian text inside log.txt in Windows-1251 encode O.o !
This good, but how i can detect console locale for windows and linux
and encode my debug information, and i'm still want save my sources in
Windows-cp1251 or UTF-8 encode ?
--
[ signature omitted ]
Message 2 in thread
Hi,
> I redirect debug out with qInstallMsgHandler() to log.txt file
> My sources saved in Windows-cp1251 encode and have russian words,
> i write program in Windows.
> When i open log.txt file i see not readable characters.
This doesn't seem related to qInstallMsgHandler(). Perhaps you could
post a minimal compilable example that reproduces the problem?
I would expect an example that attempts to write a string into a file
without involving qInstallMsgHandler().
--
[ signature omitted ]
Message 3 in thread
Here code in Windows-cp1251 encode.
#include <QtGui>
#include <QtSql>
#include "TForm.h"
#include "delegate.h"
void logOutput(QtMsgType, const char *);
QTextStream *out=0;
int main(int argc, char *argv[])
{
QTextCodec::setCodecForCStrings(
QTextCodec::codecForName("Windows-cp1251") );
if (QDir::current().exists(QLatin1String("logs")) ||
QDir::current().mkpath(QLatin1String("logs")) )
{
QFile *log = new QFile(QLatin1String("logs/log.txt"));
if ( log->open(QIODevice::WriteOnly | QIODevice::Append) )
{
out = new QTextStream(log);
qInstallMsgHandler(logOutput);
}
else
{
delete log;
qDebug("Can't open 'log.txt' file, all message will be output
to debugger and console");
}
}
QApplication app(argc, argv);
app.setQuitOnLastWindowClosed(true);
TForm Form1;
TypeDelegate delegate;
Form1.tvResult->setItemDelegate(&delegate);
//QMessageBox::information(&Form1,qApp->tr("title"),qApp->tr("open"));
Form1.show();
qDebug("ÐÐÐÑÑÐÐÐÐ ÐÑÐÐÑÐÐÐÑ");
return app.exec();
}
void logOutput(QtMsgType type, const char *msg)
{
QString debugdate =
QDateTime::currentDateTime().toString(QLatin1String("[dd.mm.yy hh:mm:ss]
"));
switch (type)
{
case QtDebugMsg:
debugdate += QLatin1String("[D]");
break;
case QtWarningMsg:
debugdate += QLatin1String("[W]");
break;
case QtCriticalMsg:
debugdate += QLatin1String("[C]");
break;
case QtFatalMsg:
debugdate += QLatin1String("[F]");
}
(*out) << debugdate << QLatin1Char(' ') << msg << endl;
}
Dimitri wrote:
> Hi,
>
>> I redirect debug out with qInstallMsgHandler() to log.txt file
>> My sources saved in Windows-cp1251 encode and have russian words,
>> i write program in Windows.
>> When i open log.txt file i see not readable characters.
>
> This doesn't seem related to qInstallMsgHandler(). Perhaps you could
> post a minimal compilable example that reproduces the problem?
>
> I would expect an example that attempts to write a string into a file
> without involving qInstallMsgHandler().
>
> --
> Dimitri
--
[ signature omitted ]
Message 4 in thread
Hi,
I just had a quick glance at the source code, since it's not a minimal
example, but this is suspect:
> qDebug("ÐÐÐÑÑÐÐÐÐ ÐÑÐÐÑÐÐÐÑ");
How this will be interpreted depends on the compiler. The compiler may
expect a source file to be encoded using UTF-8 for example.
--
[ signature omitted ]
Message 5 in thread
Hi,
> Here code in Windows-cp1251 encode.
Actually the email was UTF-8-encoded. From the mail header:
Content-Type: text/plain; charset=UTF-8; format=flowed
This is just an example of how encoding can be tricky. This is why I
think you try to build a minimal compilable example, just a few lines of
code (probably a QFile trying to write a string to a file).
--
[ signature omitted ]
Message 6 in thread
Ok, this minimal example (letter in UTF-8 but my source file in
Windows-1251):
#include <QtGui>
int main(int argc, char *argv[])
{
qDebug("ÐÑÐÐÐÑ ÐÐÑ");
return 0;
}
Inside .pro file i add CONFIGURE += console
application want only mingwm10.dll and QtCore4.dll
D:\Work\qdebug>cd release
D:\Work\qdebug\release>qdebug
âÐÑÑÑÐ ÑÑÐ
D:\Work\qdebug\release>
--
[ signature omitted ]
Message 7 in thread
SABROG a Ãcrit :
> Ok, this minimal example (letter in UTF-8 but my source file in
> Windows-1251):
>
> #include <QtGui>
>
> int main(int argc, char *argv[])
> {
> qDebug("ÐÑÐÐÐÑ ÐÐÑ");
> return 0;
> }
As I said, I suspect the compiler expects UTF-8 encoded source files.
What is the output of the following on your system, if the source file
is Windows-1251 encoded? What if the source file is UTF-8-encoded instead?
#include <iostream>
using namespace std;
int main() {
/* in the following line, the compiler inteprets a string
based on the encoding it expects in source files */
cerr << "ÐÑÐÐÐÑ ÐÐÑ" << endl;
/* in the following line, the compiler inteprets a string
independently of the encoding of the source file */
cerr << "\u041f\u0440\u0438\u0432\u0435\u0442 \u043c\u0438\u0440"
<< endl;
}
I also append the source file as a Windows-1521-encoded file to avoid
any misunderstanding.
--
[ signature omitted ]
#include <iostream>
using namespace std;
int main() {
/* in the following line, the compiler inteprets a string
based on the encoding it expects in source files */
cerr << "Ïðèâåò ìèð" << endl;
/* in the following line, the compiler inteprets a string
independently of the encoding of the source file */
cerr << "\u041f\u0440\u0438\u0432\u0435\u0442 \u043c\u0438\u0440" << endl;
}
Message 8 in thread
Dimitri ÐÐÑÐÑ:
> SABROG a Ãcrit :
>> Ok, this minimal example (letter in UTF-8 but my source file in
>> Windows-1251):
>>
>> #include <QtGui>
>>
>> int main(int argc, char *argv[])
>> {
>> qDebug("ÐÑÐÐÐÑ ÐÐÑ");
>> return 0;
>> }
>
> As I said, I suspect the compiler expects UTF-8 encoded source files.
> What is the output of the following on your system, if the source file
> is Windows-1251 encoded? What if the source file is UTF-8-encoded instead?
>
No, in this last case troubles caused just by fact that russian windows
console encoding is cp866 when source in cp1251.
"âÐÑÑÑÐ ÑÑÐ" in cp866 is exactly "ÐÑÐÐÐÑ ÐÐÑ" in cp1251
--
[ signature omitted ]
Message 9 in thread
SABROG ÐÐÑÐÑ:
> Here code in Windows-cp1251 encode.
>
> #include <QtGui>
> #include <QtSql>
>
> #include "TForm.h"
> #include "delegate.h"
>
> void logOutput(QtMsgType, const char *);
> QTextStream *out=0;
>
> int main(int argc, char *argv[])
> {
> QTextCodec::setCodecForCStrings(
> QTextCodec::codecForName("Windows-cp1251") );
> if (QDir::current().exists(QLatin1String("logs")) ||
> QDir::current().mkpath(QLatin1String("logs")) )
> {
> QFile *log = new QFile(QLatin1String("logs/log.txt"));
> if ( log->open(QIODevice::WriteOnly | QIODevice::Append) )
> {
> out = new QTextStream(log);
> qInstallMsgHandler(logOutput);
> }
> else
> {
> delete log;
> qDebug("Can't open 'log.txt' file, all message will be
> output to debugger and console");
> }
> }
> QApplication app(argc, argv);
> app.setQuitOnLastWindowClosed(true);
> TForm Form1;
> TypeDelegate delegate;
> Form1.tvResult->setItemDelegate(&delegate);
> //QMessageBox::information(&Form1,qApp->tr("title"),qApp->tr("open"));
> Form1.show();
> qDebug("ÐÐÐÑÑÐÐÐÐ ÐÑÐÐÑÐÐÐÑ");
> return app.exec();
> }
>
> void logOutput(QtMsgType type, const char *msg)
> {
> QString debugdate =
> QDateTime::currentDateTime().toString(QLatin1String("[dd.mm.yy hh:mm:ss]
> "));
> switch (type)
> {
> case QtDebugMsg:
> debugdate += QLatin1String("[D]");
> break;
> case QtWarningMsg:
> debugdate += QLatin1String("[W]");
> break;
> case QtCriticalMsg:
> debugdate += QLatin1String("[C]");
> break;
> case QtFatalMsg:
> debugdate += QLatin1String("[F]");
> }
> (*out) << debugdate << QLatin1Char(' ') << msg << endl;
> }
>
using std::ofstream instead of QTextStream helps.
--
[ signature omitted ]
Message 10 in thread
Nikolay Moskvichev ÐÐÑÐÑ:
>> (*out) << debugdate << QLatin1Char(' ') << msg << endl;
>
> using std::ofstream instead of QTextStream helps.
or change to
(*out) << debugdate << QLatin1Char(' ') <<
QString::QString::fromLocal8Bit(msg) << endl;
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("Windows-cp1251")
);
seems affects nothing here.
--
[ signature omitted ]
Message 11 in thread
Thanks ! This what im finding.
QString::QString::fromLocal8Bit(msg)
--
[ signature omitted ]
Message 12 in thread
SABROG ÐÐÑÐÑ:
> Thanks ! This what im finding.
>
Good luck! :)
> QString::QString::fromLocal8Bit(msg)
sorry, typo, QString::fromLocal8Bit(msg) of course.
--
[ signature omitted ]
Message 13 in thread
Thanks all, i found solution:
out = new QTextStream(log);
out->setCodec("ISO 8859-1");
And log file have Windows-1251 encode with my russian text :)
--
[ signature omitted ]
Message 14 in thread
On Thursday 29 March 2007 08:03, SABROG wrote:
> Thanks all, i found solution:
>
> out = new QTextStream(log);
> out->setCodec("ISO 8859-1");
>
>
> And log file have Windows-1251 encode with my russian text :)
Are you sure this actually works the way you expect? ISO8859-1 is also known
as Latin1, which is used to Western European languagees. You probably want
ISO8859-5, which is for Cyrillic languages.
I would recommend that you have a look at the follow documentation:
http://doc.trolltech.com/4.2/qtextcodec.html
http://doc.trolltech.com/4.2/tools-codecs.html
This should help you in making sure that you're using the right codec.
Good luck! :)
--
[ signature omitted ]
Message 15 in thread
Yes, i see this in QtAssistant, actually i want write
crossplatform program and i finding way how to detect
locale and normal converting for this. QTextCodec::locale ()
can return locale, but how return encoding ?
Thank for links.
Bradley T Hughes wrote:
> On Thursday 29 March 2007 08:03, SABROG wrote:
>> Thanks all, i found solution:
>>
>> out = new QTextStream(log);
>> out->setCodec("ISO 8859-1");
>>
>>
>> And log file have Windows-1251 encode with my russian text :)
>
> Are you sure this actually works the way you expect? ISO8859-1 is also known
> as Latin1, which is used to Western European languagees. You probably want
> ISO8859-5, which is for Cyrillic languages.
>
> I would recommend that you have a look at the follow documentation:
>
> http://doc.trolltech.com/4.2/qtextcodec.html
> http://doc.trolltech.com/4.2/tools-codecs.html
>
> This should help you in making sure that you're using the right codec.
>
> Good luck! :)
>
--
[ signature omitted ]
Pages: Prev | 1 | 2 | Next