Improved error reporting.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39667 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Pfeiffer
2010-11-28 08:51:58 +00:00
parent fad059587c
commit c0aa3379bb
6 changed files with 61 additions and 30 deletions
@@ -186,21 +186,26 @@ GPBinding::EndPage() throw(TransportException)
{ {
status_t status = fJob.PrintPage(fBands); status_t status = fJob.PrintPage(fBands);
DeleteBands(); DeleteBands();
if (status != B_OK) if (status == B_IO_ERROR)
throw TransportException("I/O Error"); throw TransportException("I/O Error");
if (status == B_ERROR) {
BString message;
fJob.GetErrorMessage(message);
throw TransportException(message.String());
}
} }
void status_t
GPBinding::AddBitmapToPage(BBitmap* bitmap, BRect validRect, BPoint where) GPBinding::AddBitmapToPage(BBitmap* bitmap, BRect validRect, BPoint where)
{ {
GPBand* band = new(nothrow) GPBand(bitmap, validRect, where); GPBand* band = new(nothrow) GPBand(bitmap, validRect, where);
if (band == NULL) { if (band == NULL) {
// TODO report error return B_NO_MEMORY;
return;
} }
fBands.push_back(band); fBands.push_back(band);
return B_OK;
} }
@@ -210,8 +215,9 @@ GPBinding::InitGutenprint()
if (fInitialized) if (fInitialized)
return; return;
fInitialized = true; fInitialized = true;
// TODO make sure this creates no memory leaks, // there is no "destroy" counter part so this creates memory leaks
// as there is no "destroy" counter part // this is no problem because the print server loads printer add-ons
// in a new application instance that is terminated when not used anymore
stp_init(); stp_init();
stp_set_output_codeset("UTF-8"); stp_set_output_codeset("UTF-8");
} }
@@ -48,7 +48,7 @@ public:
void EndJob() throw(TransportException); void EndJob() throw(TransportException);
void BeginPage() throw(TransportException); void BeginPage() throw(TransportException);
void EndPage() throw(TransportException); void EndPage() throw(TransportException);
void AddBitmapToPage(BBitmap* bitmap, BRect validRect, BPoint where); status_t AddBitmapToPage(BBitmap* bitmap, BRect validRect, BPoint where);
private: private:
void InitGutenprint(); void InitGutenprint();
@@ -207,6 +207,7 @@ GPDriver::endPage(int)
return true; return true;
} }
catch (TransportException& err) { catch (TransportException& err) {
ShowError(err.what());
return false; return false;
} }
} }
@@ -220,6 +221,7 @@ GPDriver::endDoc(bool)
return true; return true;
} }
catch (TransportException& err) { catch (TransportException& err) {
ShowError(err.what());
return false; return false;
} }
} }
@@ -261,25 +263,25 @@ GPDriver::nextBand(BBitmap* bitmap, BPoint* offset)
x = rc.left; x = rc.left;
y += rc.top; y += rc.top;
/*
int width = rc.right - rc.left + 1;
int height = rc.bottom - rc.top + 1;
int delta = bitmap->BytesPerRow();
DBGMSG(("width = %d\n", width));
DBGMSG(("height = %d\n", height));
DBGMSG(("delta = %d\n", delta));
DBGMSG(("renderobj->get_pixel_depth() = %d\n", fHalftone->getPixelDepth()));
*/
int width = rc.right - rc.left + 1; int width = rc.right - rc.left + 1;
int height = rc.bottom - rc.top + 1; int height = rc.bottom - rc.top + 1;
fprintf(stderr, "GPDriver nextBand x %d, y %d, width %d," fprintf(stderr, "GPDriver nextBand x %d, y %d, width %d,"
" height %d\n", " height %d\n",
x, y, width, height); x, y, width, height);
BRect imageRect(rc.left, rc.top, rc.right, rc.bottom); BRect imageRect(rc.left, rc.top, rc.right, rc.bottom);
fBinding.AddBitmapToPage(bitmap, imageRect, BPoint(x, y)); status_t status;
status = fBinding.AddBitmapToPage(bitmap, imageRect, BPoint(x, y));
if (status == B_NO_MEMORY) {
ShowError("Out of memory");
return false;
} else if (status != B_OK) {
ShowError("Unknown error");
return false;
}
} else { } else {
DBGMSG(("band bitmap is clean.\n")); DBGMSG(("band bitmap is empty.\n"));
} }
if (y >= page_height) { if (y >= page_height) {
@@ -292,8 +294,19 @@ GPDriver::nextBand(BBitmap* bitmap, BPoint* offset)
return true; return true;
} }
catch (TransportException& err) { catch (TransportException& err) {
BAlert* alert = new BAlert("", err.what(), "OK"); ShowError(err.what());
alert->Go();
return false; return false;
} }
} }
void
GPDriver::ShowError(const char* message)
{
BString text;
text << "An error occurred attempting to print with Gutenprint:";
text << "\n";
text << message;
BAlert* alert = new BAlert("", text.String(), "OK");
alert->Go();
}
@@ -42,6 +42,7 @@ protected:
bool nextBand(BBitmap* bitmap, BPoint* offset); bool nextBand(BBitmap* bitmap, BPoint* offset);
bool endPage(int page); bool endPage(int page);
bool endDoc(bool success); bool endDoc(bool success);
void ShowError(const char* message);
private: private:
GPBinding fBinding; GPBinding fBinding;
+17 -9
View File
@@ -19,7 +19,7 @@ GPJob::GPJob()
fVariables(NULL), fVariables(NULL),
fBands(NULL), fBands(NULL),
fCachedBand(NULL), fCachedBand(NULL),
fWriteError(B_OK) fStatus(B_OK)
{ {
fImage.init = ImageInit; fImage.init = ImageInit;
fImage.reset = ImageReset; fImage.reset = ImageReset;
@@ -61,7 +61,7 @@ GPJob::SetOutputStream(OutputStream* outputStream)
status_t status_t
GPJob::Begin() GPJob::Begin()
{ {
fWriteError = B_OK; fStatus = B_OK;
stp_init(); stp_init();
@@ -217,8 +217,8 @@ from72dpi(int value, int toUnit)
status_t status_t
GPJob::PrintPage(list<GPBand*>& bands) { GPJob::PrintPage(list<GPBand*>& bands) {
if (fWriteError != B_OK) if (fStatus != B_OK)
return fWriteError; return fStatus;
fPrintRect = GetPrintRectangle(bands); fPrintRect = GetPrintRectangle(bands);
fBands = &bands; fBands = &bands;
@@ -304,7 +304,14 @@ GPJob::PrintPage(list<GPBand*>& bands) {
stp_print(fVariables, &fImage); stp_print(fVariables, &fImage);
return fWriteError; return fStatus;
}
void
GPJob::GetErrorMessage(BString& message)
{
message = fErrorMessage;
} }
@@ -356,7 +363,7 @@ GPJob::Height()
stp_image_status_t stp_image_status_t
GPJob::GetRow(unsigned char* data, size_t size, int row) GPJob::GetRow(unsigned char* data, size_t size, int row)
{ {
if (fWriteError != B_OK) if (fStatus != B_OK)
return STP_IMAGE_STATUS_ABORT; return STP_IMAGE_STATUS_ABORT;
// row is relative to left, top of image // row is relative to left, top of image
@@ -454,7 +461,7 @@ GPJob::Write(const char* data, size_t size)
try { try {
fOutputStream->Write(data, size); fOutputStream->Write(data, size);
} catch (TransportException e) { } catch (TransportException e) {
fWriteError = B_IO_ERROR; fStatus = B_IO_ERROR;
} }
} }
@@ -462,8 +469,9 @@ GPJob::Write(const char* data, size_t size)
void void
GPJob::ReportError(const char* data, size_t size) GPJob::ReportError(const char* data, size_t size)
{ {
// TODO report error in printer add-on if (fStatus == B_OK)
fprintf(stderr, "GPJob Gutenprint Error: %*s\n", (int)size, data); fStatus = B_ERROR;
fErrorMessage.Append(data, size);
} }
+4 -1
View File
@@ -34,6 +34,8 @@ public:
status_t PrintPage(list<GPBand*>& bands); status_t PrintPage(list<GPBand*>& bands);
void GetErrorMessage(BString& message);
private: private:
BRect GetPrintRectangle(list<GPBand*>& bands); BRect GetPrintRectangle(list<GPBand*>& bands);
GPBand* FindBand(int line); GPBand* FindBand(int line);
@@ -75,6 +77,7 @@ private:
BRect fPrintRect; BRect fPrintRect;
list<GPBand*>* fBands; list<GPBand*>* fBands;
GPBand* fCachedBand; GPBand* fCachedBand;
status_t fWriteError; status_t fStatus;
BString fErrorMessage;
}; };
#endif #endif