From c0aa3379bbc1e1edf27d3800340a873363448b91 Mon Sep 17 00:00:00 2001 From: Michael Pfeiffer Date: Sun, 28 Nov 2010 08:51:58 +0000 Subject: [PATCH] Improved error reporting. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39667 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../print/drivers/gutenprint/GPBinding.cpp | 18 ++++++--- .../print/drivers/gutenprint/GPBinding.h | 2 +- .../print/drivers/gutenprint/GPDriver.cpp | 39 ++++++++++++------- .../print/drivers/gutenprint/GPDriver.h | 1 + .../print/drivers/gutenprint/GPJob.cpp | 26 ++++++++----- src/add-ons/print/drivers/gutenprint/GPJob.h | 5 ++- 6 files changed, 61 insertions(+), 30 deletions(-) diff --git a/src/add-ons/print/drivers/gutenprint/GPBinding.cpp b/src/add-ons/print/drivers/gutenprint/GPBinding.cpp index 4f61f47203..a6157ba0c1 100644 --- a/src/add-ons/print/drivers/gutenprint/GPBinding.cpp +++ b/src/add-ons/print/drivers/gutenprint/GPBinding.cpp @@ -186,21 +186,26 @@ GPBinding::EndPage() throw(TransportException) { status_t status = fJob.PrintPage(fBands); DeleteBands(); - if (status != B_OK) + if (status == B_IO_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) { GPBand* band = new(nothrow) GPBand(bitmap, validRect, where); if (band == NULL) { - // TODO report error - return; + return B_NO_MEMORY; } fBands.push_back(band); + return B_OK; } @@ -210,8 +215,9 @@ GPBinding::InitGutenprint() if (fInitialized) return; fInitialized = true; - // TODO make sure this creates no memory leaks, - // as there is no "destroy" counter part + // there is no "destroy" counter part so this creates memory leaks + // 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_set_output_codeset("UTF-8"); } diff --git a/src/add-ons/print/drivers/gutenprint/GPBinding.h b/src/add-ons/print/drivers/gutenprint/GPBinding.h index 2d8ce46eab..b8ba8123ec 100644 --- a/src/add-ons/print/drivers/gutenprint/GPBinding.h +++ b/src/add-ons/print/drivers/gutenprint/GPBinding.h @@ -48,7 +48,7 @@ public: void EndJob() throw(TransportException); void BeginPage() throw(TransportException); void EndPage() throw(TransportException); - void AddBitmapToPage(BBitmap* bitmap, BRect validRect, BPoint where); + status_t AddBitmapToPage(BBitmap* bitmap, BRect validRect, BPoint where); private: void InitGutenprint(); diff --git a/src/add-ons/print/drivers/gutenprint/GPDriver.cpp b/src/add-ons/print/drivers/gutenprint/GPDriver.cpp index 7018c3d7b2..bc1ac2cd22 100644 --- a/src/add-ons/print/drivers/gutenprint/GPDriver.cpp +++ b/src/add-ons/print/drivers/gutenprint/GPDriver.cpp @@ -207,6 +207,7 @@ GPDriver::endPage(int) return true; } catch (TransportException& err) { + ShowError(err.what()); return false; } } @@ -220,6 +221,7 @@ GPDriver::endDoc(bool) return true; } catch (TransportException& err) { + ShowError(err.what()); return false; } } @@ -261,25 +263,25 @@ GPDriver::nextBand(BBitmap* bitmap, BPoint* offset) x = rc.left; 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 height = rc.bottom - rc.top + 1; fprintf(stderr, "GPDriver nextBand x %d, y %d, width %d," " height %d\n", x, y, width, height); 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 { - DBGMSG(("band bitmap is clean.\n")); + DBGMSG(("band bitmap is empty.\n")); } if (y >= page_height) { @@ -292,8 +294,19 @@ GPDriver::nextBand(BBitmap* bitmap, BPoint* offset) return true; } catch (TransportException& err) { - BAlert* alert = new BAlert("", err.what(), "OK"); - alert->Go(); + ShowError(err.what()); 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(); +} diff --git a/src/add-ons/print/drivers/gutenprint/GPDriver.h b/src/add-ons/print/drivers/gutenprint/GPDriver.h index e101e13e8f..1c25fc787a 100644 --- a/src/add-ons/print/drivers/gutenprint/GPDriver.h +++ b/src/add-ons/print/drivers/gutenprint/GPDriver.h @@ -42,6 +42,7 @@ protected: bool nextBand(BBitmap* bitmap, BPoint* offset); bool endPage(int page); bool endDoc(bool success); + void ShowError(const char* message); private: GPBinding fBinding; diff --git a/src/add-ons/print/drivers/gutenprint/GPJob.cpp b/src/add-ons/print/drivers/gutenprint/GPJob.cpp index f08a70a9ff..0d79ca2735 100644 --- a/src/add-ons/print/drivers/gutenprint/GPJob.cpp +++ b/src/add-ons/print/drivers/gutenprint/GPJob.cpp @@ -19,7 +19,7 @@ GPJob::GPJob() fVariables(NULL), fBands(NULL), fCachedBand(NULL), - fWriteError(B_OK) + fStatus(B_OK) { fImage.init = ImageInit; fImage.reset = ImageReset; @@ -61,7 +61,7 @@ GPJob::SetOutputStream(OutputStream* outputStream) status_t GPJob::Begin() { - fWriteError = B_OK; + fStatus = B_OK; stp_init(); @@ -217,8 +217,8 @@ from72dpi(int value, int toUnit) status_t GPJob::PrintPage(list& bands) { - if (fWriteError != B_OK) - return fWriteError; + if (fStatus != B_OK) + return fStatus; fPrintRect = GetPrintRectangle(bands); fBands = &bands; @@ -304,7 +304,14 @@ GPJob::PrintPage(list& bands) { 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 GPJob::GetRow(unsigned char* data, size_t size, int row) { - if (fWriteError != B_OK) + if (fStatus != B_OK) return STP_IMAGE_STATUS_ABORT; // row is relative to left, top of image @@ -454,7 +461,7 @@ GPJob::Write(const char* data, size_t size) try { fOutputStream->Write(data, size); } catch (TransportException e) { - fWriteError = B_IO_ERROR; + fStatus = B_IO_ERROR; } } @@ -462,8 +469,9 @@ GPJob::Write(const char* data, size_t size) void GPJob::ReportError(const char* data, size_t size) { - // TODO report error in printer add-on - fprintf(stderr, "GPJob Gutenprint Error: %*s\n", (int)size, data); + if (fStatus == B_OK) + fStatus = B_ERROR; + fErrorMessage.Append(data, size); } diff --git a/src/add-ons/print/drivers/gutenprint/GPJob.h b/src/add-ons/print/drivers/gutenprint/GPJob.h index d997e04e89..14d89ae502 100644 --- a/src/add-ons/print/drivers/gutenprint/GPJob.h +++ b/src/add-ons/print/drivers/gutenprint/GPJob.h @@ -34,6 +34,8 @@ public: status_t PrintPage(list& bands); + void GetErrorMessage(BString& message); + private: BRect GetPrintRectangle(list& bands); GPBand* FindBand(int line); @@ -75,6 +77,7 @@ private: BRect fPrintRect; list* fBands; GPBand* fCachedBand; - status_t fWriteError; + status_t fStatus; + BString fErrorMessage; }; #endif