LLVM API Documentation

SlowOperationInformer.cpp

Go to the documentation of this file.
00001 //===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the SlowOperationInformer class for the LLVM debugger.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/SlowOperationInformer.h"
00015 #include "llvm/System/Alarm.h"
00016 #include <iostream>
00017 #include <sstream>
00018 #include <cassert>
00019 using namespace llvm;
00020 
00021 SlowOperationInformer::SlowOperationInformer(const std::string &Name)
00022   : OperationName(Name), LastPrintAmount(0) {
00023   sys::SetupAlarm(1);
00024 }
00025 
00026 SlowOperationInformer::~SlowOperationInformer() {
00027   sys::TerminateAlarm();
00028   if (LastPrintAmount) {
00029     // If we have printed something, make _sure_ we print the 100% amount, and
00030     // also print a newline.
00031     std::cout << std::string(LastPrintAmount, '\b') << "Progress "
00032               << OperationName << ": 100%  \n";
00033   }
00034 }
00035 
00036 /// progress - Clients should periodically call this method when they are in
00037 /// an exception-safe state.  The Amount variable should indicate how far
00038 /// along the operation is, given in 1/10ths of a percent (in other words,
00039 /// Amount should range from 0 to 1000).
00040 void SlowOperationInformer::progress(unsigned Amount) {
00041   int status = sys::AlarmStatus();
00042   if (status == -1) {
00043     std::cout << "\n";
00044     LastPrintAmount = 0;
00045     throw "While " + OperationName + ", operation cancelled.";
00046   }
00047 
00048   // If we haven't spent enough time in this operation to warrant displaying the
00049   // progress bar, don't do so yet.
00050   if (status == 0)
00051     return;
00052 
00053   // Delete whatever we printed last time.
00054   std::string ToPrint = std::string(LastPrintAmount, '\b');
00055 
00056   std::ostringstream OS;
00057   OS << "Progress " << OperationName << ": " << Amount/10;
00058   if (unsigned Rem = Amount % 10)
00059     OS << "." << Rem << "%";
00060   else
00061     OS << "%  ";
00062 
00063   LastPrintAmount = OS.str().size();
00064   std::cout << ToPrint+OS.str() << std::flush;
00065 }