Browse Source

Implemented trait to log erroneous results with different log levels

raster
Bergmann89 4 years ago
parent
commit
c9d71a57cf
7 changed files with 88 additions and 56 deletions
  1. +5
    -11
      space-crush-app/src/misc/text.rs
  2. +2
    -5
      space-crush-app/src/render/debug.rs
  3. +13
    -14
      space-crush-app/src/render/init.rs
  4. +8
    -13
      space-crush-app/src/render/planets.rs
  5. +8
    -13
      space-crush-app/src/render/ships.rs
  6. +50
    -0
      space-crush-common/src/misc/log_result.rs
  7. +2
    -0
      space-crush-common/src/misc/mod.rs

+ 5
- 11
space-crush-app/src/misc/text.rs View File

@@ -24,9 +24,8 @@ use glyph_brush::{
BrushAction, BrushError, FontId, GlyphBrush, GlyphBrushBuilder, GlyphVertex, OwnedSection,
OwnedText, Rectangle,
};
use log::warn;
use ordered_float::OrderedFloat;
use space_crush_common::misc::{Vfs, WorldHelper as _};
use space_crush_common::misc::{LogResult, Vfs, WorldHelper as _};
use specs::World;

use crate::{misc::WorldHelper, Error};
@@ -149,9 +148,7 @@ impl TextCache {
drop(inner);

if update_requested {
if let Err(err) = self.update() {
warn!("Unable to update text cache: {}", err);
}
self.update().error("Unable to update text cache");
}
}

@@ -347,7 +344,7 @@ fn resize_texture(new_size: (u32, u32), cur_size: Option<(u32, u32)>) -> Result<
}

fn update_texture(texture: &Texture, rect: Rectangle<u32>, data: &[u8]) {
let ret = GlcError::checked(|| {
GlcError::checked(|| {
gl::texture_sub_image_2d(
texture.id(),
0,
@@ -359,11 +356,8 @@ fn update_texture(texture: &Texture, rect: Rectangle<u32>, data: &[u8]) {
gl::UNSIGNED_BYTE,
data.as_ptr() as _,
)
});

if let Err(err) = ret {
warn!("Unable to update text texture: {}", err);
}
})
.error("Unable to update text texture");
}

fn create_vertex(data: GlyphVertex<Extra>) -> Vertex {


+ 2
- 5
space-crush-app/src/render/debug.rs View File

@@ -1,7 +1,6 @@
use std::string::ToString;

use log::warn;
use space_crush_common::resources::Global;
use space_crush_common::{misc::LogResult, resources::Global};
use specs::{ReadExpect, System};

use crate::{
@@ -88,8 +87,6 @@ where
if old != new {
*old = *new;
let s = update(old);
if let Err(err) = text.update(pos, s) {
warn!("Unable to update debug text: {}", err);
}
text.update(pos, s).error("Unable to update debug text");
}
}

+ 13
- 14
space-crush-app/src/render/init.rs View File

@@ -4,9 +4,8 @@ use glc::{
shader::{Program, Type},
vector::Vector3f,
};
use log::error;
use shrev::{EventChannel, ReaderId};
use space_crush_common::misc::WorldHelper as _;
use space_crush_common::misc::{LogResult, WorldHelper as _};
use specs::{prelude::*, ReadExpect, System, World, WriteExpect};

use crate::{
@@ -75,9 +74,9 @@ impl<'a> System<'a> for Init {

gl::viewport(0, 0, self.resolution.0 as _, self.resolution.1 as _);

if let Err(err) = camera.resize(self.resolution.0 as _, self.resolution.1 as _) {
error!("Error while updating camera: {}", err);
}
camera
.resize(self.resolution.0 as _, self.resolution.1 as _)
.error("Error while updating camera");
}

/* zoom */
@@ -90,9 +89,9 @@ impl<'a> System<'a> for Init {
* Matrix4f::scale(z.into())
* Matrix4f::translate((-state.mouse_pos.0, -state.mouse_pos.1, 0.0).into());

if let Err(err) = camera.update_with(move |v| m * v) {
error!("Error while zooming camera: {}", err);
}
camera
.update_with(move |v| m * v)
.error("Error while zooming camera");
}
}

@@ -111,14 +110,14 @@ impl<'a> System<'a> for Init {
);

let m = Matrix4f::translate(translate);
if let Err(err) = camera.update_with(move |v| m * v) {
error!("Error while moving camera: {}", err);
}
camera
.update_with(move |v| m * v)
.error("Error while moving camera");
}

if let Err(err) = uniform.update() {
error!("Error while updating global uniform data: {}", err);
}
uniform
.update()
.error("Error while updating global uniform data");

/* render background */
self.program.bind();


+ 8
- 13
space-crush-app/src/render/planets.rs View File

@@ -5,8 +5,10 @@ use glc::{
texture::Texture,
vector::Vector4f,
};
use log::error;
use space_crush_common::components::{Owned, Planet, Position};
use space_crush_common::{
components::{Owned, Planet, Position},
misc::LogResult,
};
use specs::{prelude::*, ReadExpect, ReadStorage, System, World};

use crate::{
@@ -96,19 +98,12 @@ impl<'a> System<'a> for Planets {
Vector4f::new(p_x, p_y, 0.0, 1.0),
);

if let Err(err) = self
.program
self.program
.uniform(self.location_glow_color, Uniform::Vector4f(&c))
{
error!("Error while updating glow color: {}", err);
}

if let Err(err) = self
.program
.error("Error while updating glow color");
self.program
.uniform(self.location_model, Uniform::Matrix4f(&m))
{
error!("Error while updating model matrix: {}", err);
}
.error("Error while updating model matrix");

geometry.render_quad();
}


+ 8
- 13
space-crush-app/src/render/ships.rs View File

@@ -5,8 +5,10 @@ use glc::{
texture::Texture,
vector::Vector4f,
};
use log::error;
use space_crush_common::components::{Owned, Position, Ship, ShipType, Velocity};
use space_crush_common::{
components::{Owned, Position, Ship, ShipType, Velocity},
misc::LogResult,
};
use specs::{prelude::*, ReadExpect, ReadStorage, System, World};

use crate::{
@@ -111,19 +113,12 @@ impl<'a> System<'a> for Ships {
Vector4f::new(p_x, p_y, 0.0, 1.0),
);

if let Err(err) = self
.program
self.program
.uniform(self.location_glow_color, Uniform::Vector4f(&c))
{
error!("Error while updating glow color: {}", err);
}

if let Err(err) = self
.program
.error("Error while updating glow color");
self.program
.uniform(self.location_model, Uniform::Matrix4f(&m))
{
error!("Error while updating model matrix: {}", err);
}
.error("Error while updating model matrix");

geometry.render_quad();
}


+ 50
- 0
space-crush-common/src/misc/log_result.rs View File

@@ -0,0 +1,50 @@
use std::fmt::Display;

use log::{error, warn};

pub trait LogResult {
type Ok;

fn warn(self, s: &str) -> Option<Self::Ok>;
fn error(self, s: &str) -> Option<Self::Ok>;
fn panic(self, s: &str) -> Self::Ok;
}

impl<T, E> LogResult for Result<T, E>
where
E: Display,
{
type Ok = T;

fn warn(self, s: &str) -> Option<T> {
match self {
Ok(val) => Some(val),
Err(err) => {
warn!("{}: {}", s, err);

None
}
}
}

fn error(self, s: &str) -> Option<T> {
match self {
Ok(val) => Some(val),
Err(err) => {
error!("{}: {}", s, err);

None
}
}
}

fn panic(self, s: &str) -> T {
match self {
Ok(val) => val,
Err(err) => {
error!("{}: {}", s, err);
panic!("{}: {}", s, err);
}
}
}
}

+ 2
- 0
space-crush-common/src/misc/mod.rs View File

@@ -1,7 +1,9 @@
mod log;
mod log_result;
mod vfs;
mod world;

pub use self::log::init as init_logger;
pub use self::vfs::{Vfs, VfsError};
pub use log_result::LogResult;
pub use world::{Persistence, WorldHelper};

Loading…
Cancel
Save